rafaeljusto/redigomock

Issue on redis.Bool or redis.Int commands

Closed this issue · 2 comments

func TestSomeFunc(t *testing.T) {
conn := redigomock.NewConn()
conn.Command("EXISTS","KEY").Expect(true)
conn.Command("GET","KEY2").Expect(1)
some_function(conn)
}

func some_function(conn redis.Conn) {
r, err := redis.Bool(conn.Do("EXISTS", "KEY"))
fmt.Println(r)
r, err = conn.Do("EXISTS", "KEY")
fmt.Println(r)
r, err = redis.Int(conn.Do("GET", "KEY2"))
fmt.Println(r)
r, err = conn.Do("GET", "KEY2")
fmt.Println(r)
}

Output:
false
true
0
1

Why am i getting false and 0 on applying redis.Bool and redis.Int commands & how to solve this ?

For expecting Boolean, you could use:

  • []byte("1")
  • []byte("t")
  • []byte("T")
  • []byte("true")
  • []byte("TRUE")
  • []byte("True")
  • int64(1)

For expecting an Integer, you could use:

  • []byte("123")
  • int64(123)

Check the test bellow:

package redigomockissue20

import "github.com/garyburd/redigo/redis"

func someFunction(conn redis.Conn) (result1 bool, result2 int, err error) {
    if result1, err = redis.Bool(conn.Do("EXISTS", "KEY")); err != nil {
        return
    }

    result2, err = redis.Int(conn.Do("GET", "KEY2"))
    return
}
package redigomockissue20

import (
    "testing"

    "github.com/rafaeljusto/redigomock"
)

func TestSomeFunc(t *testing.T) {
    conn := redigomock.NewConn()
    conn.Command("EXISTS", "KEY").Expect([]byte("true"))
    conn.Command("GET", "KEY2").Expect(int64(1))

    result1, result2, err := someFunction(conn)
    if err != nil {
        t.Fatal(err)
    }

    if result1 != true {
        t.Error("Unexpected boolean")
    }

    if result2 != 1 {
        t.Error("Unexpected integer")
    }
}

awesome. It worked. Thanks @rafaeljusto :)