如何期待redigomock中的非字符串值

时间:2016-07-12 06:39:17

标签: go redigo

我是redigomock的新手,通过文档和谷歌搜索,我根本无法找到一个有效的例子。我错过了一些明显的东西,但我希望我的测试能够奏效。

鉴于此代码:

var checkPasswordFailures = func(client redis.Conn, u *User) bool {
    var userkey = getPasswordFailKey(u)
    var existing, err = redis.Bool(client.Do("EXISTS", userkey))

    fmt.Printf("%s - existing = %v\n", userkey, existing)

    if err != nil {
        Log.Errorf("error in checkPasswordFailures: %s", err.Error())
    }
    if existing == true {
        fmt.Println("Existing true")
        reply, err := redis.Int(client.Do("GET", userkey))

        Log.Debug("Attempt Count: %d", reply)

        if err != nil {
            Log.Errorf("error in checkPasswordFailures: %s", err.Error())
        }
        return reply < 5
    }
    fmt.Println("Using default")
    return true
}

这个测试:

&#13;
&#13;
func TestCheckPasswordFailures_LessThan5(t *testing.T) {

	conn := redigomock.NewConn()

	conn.Command("EXISTS", "test@xxx.com_pwf").Expect(true)
	conn.Command("GET", "test@xxx.com_pwf").Expect(3)

	actual := checkPasswordFailures(conn, mockUser)

	assert.True(t, actual)

}
&#13;
&#13;
&#13;

我收到错误:

  

redigo:Bool的意外类型,得到了类型bool

我尝试了很多方法将价值强加到预期的结果中,但无济于事。

感谢任何帮助,提前谢谢!

1 个答案:

答案 0 :(得分:2)

&#13;
&#13;
func TestCheckPasswordFailures_LessThan5(t *testing.T) {

conn := redigomock.NewConn()

conn.Command("EXISTS", "test@xxx.com_pwf").Expect([]byte("true"))
conn.Command("GET", "test@xxx.com_pwf").Expect(int64(1))

actual := checkPasswordFailures(conn, mockUser)

assert.True(t, actual)
}
&#13;
&#13;
&#13;