dbones notes
its a geek thing

Rowtest with MbUnit

June 30, 2009 00:29 by Dave

I wrote a test which used the Row test attribute, which is a nice feature. This test looks at different states of a form and then tests if the correct email call is made. very similar to the previos post

[Test]
[Row(new object[] { "State Changed", "To: 2, from: 1", "bob", "message", 1, 2 })]
[Row(new object[] { "Another State Changed", "To: 3, from: 2", "bob", "message", 2, 3 })]
public void TestOfStateNtoifcation(string emailSubject, string emailMessage, string formName, string formMessage, int fromState, int toState)
{

    //Record
    var mocks = new MockRepository();
    IEmailService emailService = mocks.DynamicMock<IEmailService>();
    Expect.Call(() => emailService.SendEmail(emailSubject, emailMessage));
    mocks.ReplayAll();

    //Arrange
    Form form1 = new Form()
    {
        Name = formName,
        Message = formMessage,
        ProcessedState = (State)fromState,
    };

    IStateNotification sn = new MappedStateNotification();
    sn.SetInitalState(form1);
    sn.EmailService = emailService;

 

    //Action
    form1.ProcessedState = (State)toState;
    sn.Update(); //message was sent

    //Assert
    mocks.VerifyAll();
}

note some examples showed a Row test without the Test attribute, but Resharper does not pick up upon this... so I added it and bingo.

To pass this test, I wanted a flexible solution compared with the privious post (which used a switch statement) and came up with the use of a Dictionary. the Key would contain the To and From states, for example if the form changed from State.One to State.Two the key would look like

1->2

now the new update looks like this:

public void Update()
{
    //no change
    if (currentState == form.ProcessedState)
        return;

    //get the key
    string key = string.Format("{0}->{1}", (int)currentState, (int)form.ProcessedState);


    if (!rules.ContainsKey(key))
        return;


    NotifyRule rule = rules[key];

    EmailService.SendEmail(rule.Subject, string.Format("To: {1}, from: {0}", rule.FromState, rule.ToState));
}

VS2008 project MockingTestPart2.rar (133.36 kb)


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:
Categories: Test
Actions: E-mail | Permalink | Comments (1) | Comment RSSRSS comment feed

Related posts

Comments

June 30. 2009 06:59

Halifax Dentist

Very good source for useful tips and best guidelines for useful work.

Halifax Dentist