我正在编写我的第一个MSpec规范,并希望得到一些指导。我将规范保留在“挂起”状态,但上下文已经填写了。有什么可以改进的吗?
供参考,这是故事和第一个场景:
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 administer 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
看一下这段 MSpec 代码,注意我不得不给 MSpec 的 It
委托起了别名,因为它与 Moq.It
冲突:
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);
};
}