Ronald McDonald
Ronald McDonald

Reputation: 2911

how to unit test service call

how to test method that calls a service and passess in an object.

long AddFileDownloadEntry(FileDownloadEntry fde);

I understand how to unit test when there is some logic involved, but this service just passes in an object and the data in the object is inserted into the database.

Edit:

Sorry I wasn't clear , I'm trying to test the method in the service.

Upvotes: 1

Views: 2911

Answers (3)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236218

If responsibility of your method is calling a service and passing in an object, then you should verify, that appropriate service method was called, and appropriate object was passed.

How to do that? First of all, you should depend on abstractions (i.e. on service interface). Then you should mock this dependency and setup expectations):

FileDownloadEntry fde = // create entry
Mock<IFooService> serviceMock = new Mock<IFooService>();
serviceMock.Setup(s => s.AddFileDownloadEntry(fde)).Returns(someReturnValue);

SUT sut = new SUT(serviceMock.Object); // inject dependency
sut.YourMethod(); // act

serviceMock.VerifyAll();

This sample uses Moq testing library.

BTW by default Moq will compare passed arguments by reference. If you want them to be compared by value, you should override Equals on FileDownloadEntry, or verify argument manually.

Upvotes: 1

Jarrett Meyer
Jarrett Meyer

Reputation: 19573

Generally, you cannot unit test these integration points. You can wrap the method call with an adapter, and you can test the usage of the adapter, but the actual call won't be covered by a test. That's just what happens at integration points - eventually you need to call services, talk to databases, work with file systems, etc.

Upvotes: 1

Johan Larsson
Johan Larsson

Reputation: 17580

I suggest mocking the service and verify that:

  • The expected method is called the expected number of times
  • The expected method is called with the expected argument.

Upvotes: 1

Related Questions