This is a migrated thread and some comments may be shown as answers.

Functions mocked in the constructor are removed when mocking other functions from the same class within a test method

3 Answers 50 Views
JustMock Free Edition
This is a migrated thread and some comments may be shown as answers.
Allan
Top achievements
Rank 1
Allan asked on 11 Jun 2015, 10:19 PM

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();
}
}

3 Answers, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 16 Jun 2015, 09:07 AM
Hello Allan,

It appears that the xunit assembly name changed from "xunit" to "xunit.core" which broke our integration. I've logged a bug and it should be fixed in one of our upcoming releases.

As a token of gratitude for reporting this issue to us, I've given you some Telerik points.

Regards,
Stefan
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Allan
Top achievements
Rank 1
answered on 16 Jun 2015, 07:50 PM

Thanks for the follow up and points Stefan!  In case you need more information about the recent changes to xUnit, please see their reply to my post.  https://github.com/xunit/xunit/issues/434

0
Stefan
Telerik team
answered on 17 Jun 2015, 01:10 PM
Hello Allan,

Thanks for all the help. I've already pushed support for Xunit 2.x. It will take awhile to get into the release channel, until then you can try it by compiling the master branch in our GitHub repo.

Regards,
Stefan
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
JustMock Free Edition
Asked by
Allan
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Allan
Top achievements
Rank 1
Share this question
or