Hi there,
I am new to Mocking and somewhat familiar with unit testing and finally have decided to bite the bullet on a new project starting up front with a strict TDD approach. However I have one service class and method I need to retrospectively add tests to as it has been promoted from a prototype.
I do not know where to start though on this particular test, this is the classes and methods involved:
public class PageService : IPageService
{
private readonly ITestService testServiceClient;
public PageService(ITestService testServiceClient)
{
this.testServiceClient = testServiceClient;
}
public Page GetPage(Guid websiteId, string pageKey)
{
Page builtPage = null;
// WCF SERVICE CALL I DO NOT WANT EXECUTING WHEN RUNNING UNIT TEST
// BUT RATHER WANT A BLANK NEW INSTANCE OF "PAGE" CREATED USING MOQ??
var page = testServiceClient.GetPage(websiteId, pageKey);
if (page == null)
return null;
builtPage = new Page();
[code here to build builtPage if input params ok] ...
return builtPage;
}
}
What I am endeavouring to do, is write one test, from this hopefully I can expand out all permutations of GetPage(...)
tests, but for the first one simply test if a valid websiteId
and pageKey
has been passed in, if so receive a valid Page
instance back and assert true to the unit test.
testServiceClient
is a WCF service client, this was wrapped in a using()
statement before, but I have moved it out of that in hope to get it going with dependency injection as I feel that will be the first step required for testing, as from what I understand, I will need to give it a fake / mocked? wcf client instead where testServiceClient.GetPage()
returns a known in-memory set of data? Hopefully I am on the right track there.
So here is my initial blueprints of my unit test (I am using Moq framework, Setup()
is the new version of Expect()
if you haven't used latest ver of Moq):
/// <summary>
/// Tests service returns a valid instance of type `Page` when a valid website and valid page key is tested.
/// </summary>
[TestMethod]
public void Test_Valid_Website_And_Valid_PageKey_Returns_Valid_Instance_Of_Page()
{
// Arrange
Page page = null;
// Act
var newPage = new Page() { Title = "Mocked Version!?"};
testServiceClient = new Mock<ITestService>();
testServiceClient.Setup(x => x.GetPage(websiteId, "about-us")).Returns(newPage);
service = new PageService(testServiceClient.Object);
page = service.GetPage(websiteId, "about-us");
// Assert
Assert.IsInstanceOfType(page, typeof(Page), "Object was not of expected instance type.");
}
I have no idea where to go from here, or if I am on the right track? I can confirm the above has no syntax errors, and I do receive an exception:
System.ArgumentException: Invalid setup on a non-overridable member:
x => x.GetPage(websiteId, "about-us").
All I know is that I want my service.GetPage(...)
to return a new instance, as websiteId
and pageKey
was valid, however I don't want it to use the real testServiceClient.GetPage()
WCF call... hopefully I understand the idea of mocking correctly. Have I told it correctly through Moq when you use testServiceClient.GetPage
on that service, actually just return a new instance of page?
Any clarification is greatly appreciated! Thanks guys!