Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why mock with no expectations not failing #12

Open
henrikrudstrom opened this issue May 20, 2021 · 6 comments
Open

Why mock with no expectations not failing #12

henrikrudstrom opened this issue May 20, 2021 · 6 comments

Comments

@henrikrudstrom
Copy link

This package looks awesome, but i would have expected these tests to fail:

package frontpage_test

import (
	"context"
	"testing"

	"github.com/go-redis/redismock/v8"
	. "github.com/onsi/ginkgo"

	. "github.com/onsi/gomega"
)

func TestFrontpage(t *testing.T) {
	RegisterFailHandler(Fail)
	RunSpecs(t, "Frontpage Suite")
}

var _ = FDescribe("Reader", func() {
	It("should fail", func() {
		redisCli, redisMock := redismock.NewClientMock()
                 // No expectations set, so this should fail right?
		_ = redisCli.Set(context.TODO(), "key", "value", 0).Err()

		Expect(redisMock.ExpectationsWereMet()).To(BeNil())
	})

	It("should fail", func() {
		redisCli, redisMock := redismock.NewClientMock()
                 // just setting an expectation that is fullfilled, to see if that helps...
		redisMock.ExpectPing()
		redisCli.Ping(context.TODO())
		_ = redisCli.Set(context.TODO(), "key", "value", 0).Err()

		Expect(redisMock.ExpectationsWereMet()).To(BeNil())
	})
})

Im calling a Set that is not expected, why is that not failing?

@henrikrudstrom henrikrudstrom changed the title Why is this not failing Why mock with no expectations not failing May 20, 2021
@monkey92t
Copy link
Member

All expected commands have been executed.

@henrikrudstrom
Copy link
Author

ok, but shouldnt it fail, or, it would be nice to set it up to fail if a command is executed that should not be executed. In my case i want to ensure nothing is written to redis in certain cases. is there a way to set that up?

@monkey92t
Copy link
Member

You will receive the error returned by the command.

// fail
err = redisCli.Set(context.TODO(), "key", "value", 0).Err()
Expect(err).NotTo(HaveOccurred())

@henrikrudstrom
Copy link
Author

ah. thats how it is intended. Unfortunately that doesnt solve my case. the method im testing is handling the error (and not returning it), so i wont be able to assert that Set was never called.

i like the behaviour of testify (https://github.com/stretchr/testify). it panics when an unexpected method is called, causing the test to fail.

Would be nice to be have a toggle that panics instead of returning an error mock.PanicOnUnexpectedCall(true) or somthing. Happy to provide a PR for that. (i guess its a matter of adding an if statement around here: https://github.com/go-redis/redismock/blob/master/mock.go#L153)

@qa-simon-heuser
Copy link

Running into the same issue. It's quite confusing that the mock does not fail on unexpected method calls.

@antonyho
Copy link
Contributor

Can we add a function for reporting unexpected command calls into the Expect interface?

Here is my proposal Pull Request: #91

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants