How is the unit test method waiting to update an object using a link?

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); //saves the updated policy details via nhibernate
}

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)); //this fails      
}

, , , .

, , - InactivatePolicy .

public Policy InactivatePolicy(Policy policy)
{
    policy.Status = Inactive;
    UpdatePolicy(policy); //saves the updated policy details via nhibernate
    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)); //this now succeeds

}

, unit test, , - .

- , ? , , , ?

, InactivatePolicy, Policy ?

+2
3

PolicyManager, UpdatePolicy, DoSomething.

, , , .

InactivatePolicy() , DoSomething(), "".

2 , , .

+6

Rambling

.

, true NHibernate (.. , ), ? , , , , ? , true, , .

, "" ; nHibernate, . UpdatePolicy "MockRepo" "nHibernateRepo".

, , , .

, , , nHibernate , , , .

test dev db, ?

0

, , , , PolicyManager - DoSomething . , PolicyManager UpdateInactive unit test DoSomething PolicyManager.

0

All Articles