the_drow
the_drow

Reputation: 19181

Test does not fail at first run

I have the following test:

[Test]
public void VerifyThat_WhenProvidingAServiceOrderWithALinkedAccountGetSerivceProcessWithStatusReasonOfEndOfEntitlementToUpdateStatusAndStopReasonForAccountGetServiceProcessesAndServiceOrders_TheProcessIsUpdatedWithAStatusReasonOfEndOfEntitlement()
{
    IFixture fixture = new Fixture()
                           .Customize(new AutoMoqCustomization());
    Mock<ICrmService> crmService = new Mock<ICrmService>();

    fixture.Inject(crmService);

    var followupHandler = fixture.CreateAnonymous<FollowupForEndOfEntitlementHandler>();
    var accountGetService = fixture.Build<EndOfEntitlementAccountGetService>()
                                   .With(handler => handler.ServiceOrders, new HashedSet<EndOfEntitlementServiceOrder>
                                           {
                                               {
                                                  fixture.Build<EndOfEntitlementServiceOrder>()
                                                         .With(order => order.AccountGetServiceProcess, fixture.Build<EndOfEntitlementAccountGetServiceProcess>()
                                                                                                           .With(process => process.StatusReason, fixture.Build<StatusReason>()
                                                                                                                                                             .With(statusReason=> statusReason.Id == MashlatReasonStatus.Worthiness)
                                                                                                                                                            .CreateAnonymous())
                                                                                                                .CreateAnonymous())
                                                          .CreateAnonymous()
                                               }
                                           })
                                          .CreateAnonymous();
    followupHandler.UpdateStatusAndStopReasonForAccountGetServiceProcessesAndServiceOrders(accountGetService);

    crmService.Verify(svc => svc.Update(It.IsAny<DynamicEntity>()), Times.Never());
}

My problem is that it will never fail on the first run, like TDD specifies that it should.
What it should test is that whenever there is a certain value to a status for a process of a service order, perform no updates.
Is this test checking what it should?

Upvotes: 0

Views: 154

Answers (3)

bryanbcook
bryanbcook

Reputation: 18303

Following TDD methodology, we only write new tests for functionality that doesn't exist. If a test passes on the first run, it is important to understand why.

One of my favorite things about TDD is its subtle ability to challenge our assumptions, and knock our egos flat. The practice of "Calling your Shots" is not only a great way to work through tests, but it's also a lot of fun. I love when a test fails when I expect it to pass - many great learning opportunities come from this; Time after time, evidence of working software trumps developer ego.

When a test passes when I think it shouldn't, the next step is to make it fail.

For example, your test, which expects that something doesn't happen, is guaranteed to pass if the implementation is commented out. Tamper with the logic that you think you are implementing by commenting it out or by altering the conditions of the implementation and verify if you get the same results.

If after doing this, and you're confident that the functionality is correct, write another test that proves the opposite. Will Update get called with different state or inputs?

With both sets in place, you should be able to comment out that feature and have the ability to know in advance which test will be impacted. (8-ball, corner pocket)

I would also suggest that you add another assertion to the above test to ensure that the subject and functionality under test is actually being invoked.

Upvotes: 1

Gishu
Gishu

Reputation: 136643

I'm struggling a bit to understand the question here...

Is your problem that this test passes on the first try? If yes, that means one of two things

  • your test has an error
  • you have already met this spec/requirement

Since the first has been ruled out, Green it is. Off you go to the next one on the list..

Somewhere down the line I assume, you will implement more functionality that results in the expected method being called. i.e. when the status value is different, perform an update. The fix for that test must ensure that both tests pass.

If not, give me more information to help me understand.

Upvotes: 1

none
none

Reputation: 4847

change the Times.Never() to Times.AtLeastOnce() and you got a good start for tdd.

Try to find nothing in nothing, well that's a good test ,but not they way to start tdd, first go with the simple specification, the naive operation the user could do (from your view point of course).

As you done some work, keep it for later, when it fails.

Upvotes: 0

Related Questions