Reputation: 121
I want to write unit test for this FirstPageLoad method. This is a void method so i don't know how to write a unit tests for this void methods.
public class PagingSevice : IPagingService
{
private EventHandler<PageEventArgsHelper> pageEventArgs;
private List<string> PageGuids;
public void FirstPageLoad()
{
SetPageLoading(0);
}
public void SetGuids(List<string> pageGuids)
{
PageGuids = pageGuids;
}
private void SetPageLoading(int pageNumber)
{
if (!PageGuids.Any())
{
PageEventArgsHelper page = new PageEventArgsHelper(string.Empty, true, true);
pageEventArgs?.Invoke(this, page);
return;
}
bool isFirst = false;
bool isLast = false;
if (pageNumber == 0)
{
isFirst = true;
}
if (PageGuids.Count -1 == pageNumber)
{
isLast = true;
}
PageEventArgsHelper pageArgs = new PageEventArgsHelper(PageGuids[pageNumber], isFirst, isLast);
pageEventArgs?.Invoke(this, pageArgs);
}
}
can i use moq.verify to test this method?
Upvotes: 0
Views: 5842
Reputation: 218847
Based on comments on the question above...
Is there an observable side-effect from SetPageLoading(0)? -> No
Does it modify anything in the object which can be seen externally? -> No
Does it modify anything in a dependency? -> No
It sounds like the only observation that can be made about the result is "no exception is thrown". For that you don't need to mock anything or even assert anything. In any testing framework, an unexpected exception fails the test. So as long as there's no exception, the test passes. Something as simple as:
// arrange
var service = new PagingService();
// act
service.FirstPageLoad();
// assert
// no exception is thrown
Now, not knowing much about what this object does, if something were to fail in this case because there's an external dependency that isn't met, then that would be the externally observable side-effect.
I see no constructor or any way to inject a dependency. So if there is one then building the unit test(s) is an opportunity to invert that dependency so it can be provided to the object. But if there isn't one and everything happens privately within the object then the functionality is a black box and all that can be observed is the lack of an exception.
Upvotes: 0
Reputation: 169210
There is nothing special about a method that returns void in this context. You would just call the method and use the Verify
method to verify that it was called as usual, e.g.:
var mock = new Mock<IPagingService>();
mock.Object.FirstPageLoad();
mock.Verify(x => x.FirstPageLoad(), Times.Once);
You may also want to verify that the event was raised by the SetPageLoading
method when you called the public FirstPageLoad
method.
Upvotes: 1