HexaCrop
HexaCrop

Reputation: 4293

How to mock libraries used inside service/controller while running cucumber

I am using cucumber to run acceptance tests. I am using the following Zendesk library to call methods to create tickets, and view them https://github.com/nukosuke/go-zendesk

I want to know while writing acceptance tests how to mock this library. I am using dependency injection design to test this on a unit test case. But I don't know how I can mock it while acceptance tests.

I know that in case of acceptance tests, you should actually run it without mocking, but let's say that there is cost associated with this zendesk api for now, and I don't want to actually call zendesk api behind the scenes.

How can i mock the library here?

Thanks

Upvotes: 1

Views: 159

Answers (1)

Adrian
Adrian

Reputation: 46562

You cannot mock a library or module or package, you can only mock a type.

Mocking any type works the same way:

  1. Create an interface that covers the methods you use from that type
  2. Change functions that take that type to take the interface instead
  3. Create a mock type that satisfies the interface for use in testing
  4. Pass the real type in production code, and the mock in test code

Upvotes: 1

Related Questions