Currently we are using JustMock Lite with xUnit 1.9.2 and have test cases where we create reusable mocks for our test methods within the controller of a test class. My team bumped into an issue after performing an upgrade to version 2.0. It appears when using the constructor to mock a method from a class (for reuse in test methods) and then creating a second mocked function within the test method (which is a different method from the same class that was defined in the constructor) causes the mocked implementation of the method in the constructor to disappear.
Please see the test example below. In the test class MockIssueExample, I have test methods that verifies the return value from an interface method or virtual function. In version 1.9.2, the mocks defined in SetupMocks() returns the value of 5 within each of the test methods. When I upgrade to version 2.0, each of the mocked methods returns a value of 0 in the test method and causes my test to fail.
Note: In each of the test methods I left a line of code that fixes the issue but it requires me to copy the mocked implementation from the constructor into the test method.Has something changed in 2.0 that's causing this scoping issue? Can we mock one function within a constructor and a second method within the actual test method without losing the mock implementation that what was defined in the constructor.
Please Advise. Thanks!
public class MockIssueExample
{
private ITestMethods _testMethods;
private ClassUnderTest _classUnderTest;
private ClassTestMethods _classTestMethods;
public MockIssueExample()
{
SetupMocks();
_classUnderTest = new ClassUnderTest(_testMethods, _classTestMethods);
}
private void SetupMocks()
{
_testMethods = Mock.Create<ITestMethods>();
_classTestMethods = Mock.Create<ClassTestMethods>();
Mock.Arrange(() => _testMethods.FunctionReturnsInteger_1()).Returns(5);
Mock.Arrange(() => _classTestMethods.VirtualFunctionReturnsInteger_1()).Returns(5);
}
[Fact]
public void Virtual_function_1_should_return_the_mocked_value()
{
//Mock.Arrange(() => _classTestMethods.VirtualFunctionReturnsInteger_1()).Returns(5);
Mock.Arrange(() => _classTestMethods.VirtualFunctionReturnsInteger_2()).Returns(6);
int returnVal = _classUnderTest.GetValueReturnedFromVirtualFunction_1();
Assert.True(returnVal == 5);
}
[Fact]
public void Interface_function_1_should_return_the_mocked_value()
{
//Mock.Arrange(() => _testMethods.FunctionReturnsInteger_1()).Returns(5);
Mock.Arrange(() => _testMethods.FunctionReturnsInteger_2()).Returns(9);
int returnVal = _classUnderTest.GetValueReturnedFromInterfaceFunction_1();
Assert.True(returnVal == 5);
}
}
public interface ITestMethods
{
int FunctionReturnsInteger_1();
int FunctionReturnsInteger_2();
}
public class ClassTestMethods
{
public virtual int VirtualFunctionReturnsInteger_1()
{
return 1;
}
public virtual int VirtualFunctionReturnsInteger_2()
{
return 2;
}
}
public class ClassUnderTest
{
private readonly ITestMethods _testMethods;
private readonly ClassTestMethods _classTestMethods;
public ClassUnderTest(ITestMethods testMethods, ClassTestMethods classTestMethods)
{
_testMethods = testMethods;
_classTestMethods = classTestMethods;
}
public int GetValueReturnedFromVirtualFunction_1()
{
return _classTestMethods.VirtualFunctionReturnsInteger_1();
}
public int GetValueReturnedFromInterfaceFunction_1()
{
return _testMethods.FunctionReturnsInteger_1();
}
}