rafaeljusto/redigomock

LLEN and LINDEX not being recognized

Closed this issue · 2 comments

It seems like RPUSH, GET commands work as intended.

conn.Do("RPUSH", "key", object) // Works fine
conn.Do("LLEN, "key", object) // Not working

LLEN and LINDEX throws an error, "registered in redigomock library"

I could not reproduce the error. Also, in your example you're using 2 parameters in the LLEN command, where you suppose to use 1 according to the documentation. The scenario that I used:

redigomock-issue32.go

package main

import (
	"fmt"
	"os"

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

const (
	redisAddress = "127.0.0.1:6379"
)

// LLENExample follows the example from https://redis.io/commands/llen
func LLENExample(c redis.Conn) (int, error) {
	if _, err := c.Do("LPUSH", "mylist", "World"); err != nil {
		return 0, err
	}

	if _, err := c.Do("LPUSH", "mylist", "Hello"); err != nil {
		return 0, err
	}

	return redis.Int(c.Do("LLEN", "mylist"))
}

func main() {
	c, err := redis.Dial("tcp", redisAddress)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	defer c.Close()

	ouput, err := LLENExample(c)
	if err != nil {
		fmt.Println(err)
		os.Exit(2)
	}

	fmt.Println(ouput)
}

redigomock-issue32_test.go

package main

import (
	"reflect"
	"testing"

	"github.com/rafaeljusto/redigomock"
)

func TestLLENExample(t *testing.T) {
	c := redigomock.NewConn()

	scenarios := []struct {
		description   string
		stub          func()
		expected      int
		expectedError error
	}{
		{
			description: "it should retrieve the list size correctly",
			stub: func() {
				c.Command("LPUSH", "mylist", "World").Expect(int64(1))
				c.Command("LPUSH", "mylist", "Hello").Expect(int64(2))
				c.Command("LLEN", "mylist").Expect(int64(2))
			},
			expected: 2,
		},
	}

	for _, scenario := range scenarios {
		t.Run(scenario.description, func(t *testing.T) {
			c.Clear()
			scenario.stub()

			output, err := LLENExample(c)

			if !reflect.DeepEqual(scenario.expected, output) {
				t.Errorf("unexpected messages. Expected “%#v” and got “%#v”", scenario.expected, output)
			}

			if !reflect.DeepEqual(scenario.expectedError, err) {
				t.Errorf("unexpected error. Expected “%#v” and got “%#v”", scenario.expectedError, err)
			}
		})
	}
}

Could you create a test case where this problem occurs? Usually the message command XXXXX with arguments YYYY not registered in redigomock library happens when you forget to add the expectation in the mock library.

Sorry it was due to the fact that I wasn't calling command before I pass the mock to the function. It was my misunderstanding of the usage.