Community Choice awards

Published 6/30/2009 by Dave in General

Looks like source forge have opened the voting for your favorite projects/programs.

Place your vote

 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Rowtest with MbUnit

Published 6/30/2009 by Dave in Test
Tags:

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

One thing I wanted to do was to ensure my code was calling a function with the correct inputs. However the function did not return anything (void) and I did not want to write a mock object from scratch which had a verify method was called (that seemed like re-writing the wheel). So I looked a little into Rhino Mocks (Ayende, you are a genius!), low and behold you can do this with this framework (quite easily)

Project Files TestVoidMethodMocking.rar (128.38 kb)  (VS 2008, using MBUnit)

Ok lets quickly look at what I am doing. I have a Interface, for sending emails.

public interface IEmailService
{
    void SendEmail(string subject, string body);
}

And I have a class which has an Update() method, which calls this method (when the State changes from State.One to State.Two), as follows

public void Update()
{
    switch (currentState)
    {
        case State.One:
            if (form.ProcessedState == State.Two)
            {
                EmailService.SendEmail("Status Change", string.Format("Form {0} has entered state 2", form.Name));
            }
            break;
        case State.Two:
            break;
        case State.Three:
            break;
        default:
            break;
    }
}

Within the Test project, I would like to verify that the SendEmail() method is being called with the correct information. So I have used Rhino Mocks Expect.Call to ensure this. For this test I am going to have a form.Name = "test", so I will expect the method call to look like

SendEmail("Status Change", "Form test has entered state 2")

Here is the Unit Test Code: 

[Test]
public void TestIfEmailWasCalledCorrectly()
{
    //Record
    var mocks = new MockRepository();
    IEmailService emailService = mocks.DynamicMock<IEmailService>();
    Expect.Call(() => emailService.SendEmail("Status Change", "Form test has entered state 2"));
    mocks.ReplayAll();

    //Arrange
    Form form1 = new Form()
    {
        Name = "test",
        Message = "test1",
        ProcessedState = State.One,
    };

    StateNotification sn = new StateNotification(form1);
    sn.EmailService = emailService;

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

    //Assert
    mocks.VerifyAll();
}

 

now when I run the test. I get all green

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

A Little more on the MVC Routing

Published 6/22/2009 by Dave in asp.net
Tags:

Couple of links (Which I need to have a further read of)

Legacy Url Routing - converting your ASP.net application to a MVC, how to route the old aspx page to redirect to the MVC required view.

Asp.Net Routing Debugger - allows you to type in URLs and it shows which routes match it (nice :) )

three Common ASP.NET MVC URL Routing Issues - as title suggests

(in a nut shell)

  1. Register the mappings in the correct order
  2. Set the redirect of the default.aspx
  3. parameter names match

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Routing - my simple test

Published 6/22/2009 by Dave in asp.net
Tags: ,

ASP.NET MVC comes with a routing engine, and from my understanding this will be applied to ASP.Net (I may be wrong on that). however thats not the point of this post. Today I wanted to note down some simple information about the routing engine.

The routing by default is located in the Global.asax

routes.MapRoute(
    "Default",                                              // Route name
    "{controller}/{action}/{id}",                           // URL with parameters
    new { controller = "Home", action = "Index", id = ""// Parameter defaults
);

The route name must be unique
the URL can contain params which are denoted by the curly brackets
finally the default values (for this mapping)

Now lets add a Mapping, I want to be able to goto http://Mydomain/pages/aboutus, the pages just signifies that these are the pages for this website, but the last part i want to be the page name (and thus this should be able to be altered, for example, pages/contactus, or pages/someotherpage.)

The steps (If I want to use another controller, PageController.cs)

1. I think i will create the mapping first

routes.MapRoute(
    "Pages",                                //the name
    "Pages/{pageName}",                     //the url, note 1 param
    new {Controller= "Page", action="Index"}//the defaults
);

Ok you will note the URL does not contain the Controller name ("Page" in this instance) and nether the action ("Index"). these are being set by the defaults.

2. Create the controller (PageController.cs), by right clicking on the controller folder and selecting "New Controller"

public class PageController : Controller
{
    //
    // GET: /Page/

    public ActionResult Index(string pageName)
    {
        //some logic here
        return View();
    }

}

now if you add a break point at the point of the Index method, and browse to the http://myDomain/pages/yourpage, you will see the pageName param contains "yourpage". Fantastic!

If i wanted (out of curiosity) how can i just use the Home controller (provided) and just add a method called Page, I could do something like this

routes.MapRoute(
    "Pages",
    "Pages/{pageName}",
    new {Controller= "Home", action="Page"}
);

note all i had to do is change the controller and action on the default object (nice and simple), here is the method i added to the HomeController.cs

public ActionResult Page(string pageName)
{
    ViewData["Message"] = string.Format("you are veiwing the {0} page", pageName);
    return View();
}

this too populated the pageName param, which then allow me to do some logic and maybe find the data for the requested page (wahey)

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5