您的代碼有問題。你的行爲取決於一個靜態方法:Membership.CreateUser
。正如你所知,靜態方法是單元測試的PITA。
所以,你可以通過引入一個抽象層削弱耦合:
public interface IMyService
{
MembershipCreateStatus CreateUser(string username, string password, string email);
}
,然後有一些實現,將使用目前的會員提供:
public class MyService: IMyService
{
public MembershipCreateStatus CreateUser(string username, string password, string email)
{
MembershipCreateStatus status;
Membership.CreateUser(username, password, email, null, null, true, null, out status);
return status;
}
}
終於控制器:
public class AccountController : Controller
{
private readonly IMyService _service;
public AccountController(IMyService service)
{
_service = service;
}
[HttpPost]
public ActionResult Register(RegisterModel model)
{
if (ModelState.IsValid)
{
// Attempt to register the user
var status = _service.CreateUser(model.UserName, model.Password, model.Email);
if (status == MembershipCreateStatus.Success)
{
FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */);
return RedirectToAction("Index", "Home");
}
else
{
ModelState.AddModelError("", ErrorCodeToString(createStatus));
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
}
好吧,現在我們已經削弱了我們可以聯繫e嘲笑框架來嘲笑單元測試中的服務並使其變得微不足道。
例如使用犀牛製品,你可以創建下面的測試覆蓋2失敗的案例:
[TestMethod]
public void Register_Action_Should_Redisplay_View_If_Model_Is_Invalid()
{
// arrange
var sut = new AccountController(null);
var model = new RegisterModel();
sut.ModelState.AddModelError("", "invalid email");
// act
var actual = sut.Register(model);
// assert
Assert.IsInstanceOfType(actual, typeof(ViewResult));
var viewResult = actual as ViewResult;
Assert.AreEqual(model, viewResult.Model);
}
[TestMethod]
public void Register_Action_Should_Redisplay_View_And_Add_Model_Error_If_Creation_Fails()
{
// arrange
var service = MockRepository.GenerateStub<IMyService>();
service
.Stub(x => x.CreateUser(null, null, null))
.IgnoreArguments()
.Return(MembershipCreateStatus.InvalidEmail);
var sut = new AccountController(service);
var model = new RegisterModel();
// act
var actual = sut.Register(model);
// assert
Assert.IsInstanceOfType(actual, typeof(ViewResult));
var viewResult = actual as ViewResult;
Assert.AreEqual(model, viewResult.Model);
Assert.IsFalse(sut.ModelState.IsValid);
}
最終的測試是成功的情況下。我們仍然有一個問題。問題是以下行:
FormsAuthentication.SetAuthCookie(model.UserName, false);
這是什麼?這是一個靜態方法調用。因此,我們採用與成員資格提供商相同的方式來削弱我們的控制器和表單認證系統的耦合。
+1爲偉大和容易遵循步行。 – ljubomir 2012-04-26 08:52:28