Hi guys,
Just wondering if any MSpec and BDDers out there could give me there thoughts on my first attempt at writing a MSpec spec. Now I've left the specs uncoded, but my context has code in it, I just want to know if I'm heading along the right lines, or if there are any improvements to be made with what I've done.
As this can be subjective, I've marked it community wiki.
Right, first, here is my story and first scenario:
Story: "Blog admin logs in to the system"
As a blog writer
I want to be able to log in to my blog
So that I can write posts and administrate my blog
Scenario: "Logs in from the login page"
Given the user enters in correct credentials for a user in the system
When the user clicks the "Login" button
Then log the user in and redirect to the admin panel with a message
stating that he logged in correctly
and heres my MSpec code:
using System;
using Machine.Specifications;
using Machine.Specifications.Model;
using Moq;
using MyBlog.Controllers;
using System.Web.Mvc;
using MoqIt = Moq.It;
using ThenIt = Machine.Specifications.It;
[Subject("User tries logging in")]
public class When_user_enters_valid_credentials : With_user_existing_in_membership
{
protected static ActionResult result;
Because of = () =>
{
result = loginController.Login(validUsername, validPassword);
};
ThenIt should_log_the_user_in;
ThenIt should_redirect_the_user_to_the_admin_panel;
ThenIt should_show_message_confirming_successful_login;
}
public abstract class With_user_existing_in_membership
{
protected static Mock<ISiteMembership> membershipMock;
protected static string validUsername;
protected static string validPassword;
protected static LoginController loginController;
Establish context =()=>
{
membershipMock = new Mock<ISiteMembership>();
validUsername = "ValidUsername";
validPassword = "ValidPassword";
//make sure it's treated as valid usernames and password
membershipMock.Setup<bool>(m => m.Validate(MoqIt.Is<string>(s => s == validUsername), MoqIt.Is<string>(s => s == validPassword)))
.Returns(true);
loginController = new LoginController(membershipMock.Object);
};
}
Now I've had to do the aliasing of the It class and delegate because of Moq and MSpec conflicting, but I hope it still reads well.
Be happy to hear you thoughts.