I'm having a problem testing a method that changes some properties of the reference type that are passed to it.
As an example, suppose I have a class called Policy.
Policy policy = new Policy();
policy.Status = Active;
I then pass this policy to the policy manager to inactivate the policy.
policyManager.InactivatePolicy(policy);
An invalid policy method performs the following actions:
public void InactivatePolicy(Policy policy)
{
policy.Status = Inactive;
UpdatePolicy(policy);
}
I am having problems with a module testing this DoSomething method. (ignore the fact that what he does in this example is useless)
public void DoSomething(Policy policy)
{
Policy policy = new Policy();
policy.Status = Active;
policyManager.InactivatePolicy(policy);
}
Since I mock the policy manager, the status is not set to inactive and as a result, when I say that after DoSomething is called the status, the policy is inactive. I am getting a test failure because it is still active.
[Test]
public void TheStatusShouldBeInactiveWhenWeDoSomething()
{
Mock<IPolicyManager> policyManagerMock = new Mock<PolicyManager>();
MyClass mc = new MyClass(policyManagerMock.Object);
Policy policy = new Policy();
policy.Status = Active;
mc.DoSomething(policy);
Assert.That(policy.Status, Is.EqualTo(Inactive));
}
, , , .
, , - InactivatePolicy
.
public Policy InactivatePolicy(Policy policy)
{
policy.Status = Inactive;
UpdatePolicy(policy);
return policy;
}
[Test]
public void TheStatusShouldBeInactiveWhenWeDoSomething()
{
Mock<IPolicyManager> policyManagerMock = new Mock<PolicyManager>();
MyClass mc = new MyClass(policyManagerMock.Object);
Policy expectedInactivePolicy = new Policy();
expectedInactivePolicy.Status = Inactive;
Policy policy = new Policy();
policy.Status = Active;
policyManagerMock
.Setup(p => p.InactivatePolicy(policy))
.Returns(expectedInactivePolicy);
mc.DoSomething(policy);
Assert.That(policy.Status, Is.EqualTo(Inactive));
}
, unit test, , - .
- , ? , , , ?
, InactivatePolicy, Policy ?