MSpec을 처음 사용하고 ASP.NET MVC 테스트를 작성한 방법이 올바른지 알고 싶습니다. 테스트는 통과하지만 필자가 작성한 방식이 맘에 들지 않으며 어색하게 보입니다. 나는 확실히 뭔가를 놓친다. 사전에MSpec을 사용한 ASP.NET MVC 테스트. 이것이 맞습니까, 아니면 리팩토링이 필요합니까?
public class AccountControllerTests3
{
protected static AccountController controller;
static IFormsAuthenticationService formsService;
static IMembershipService membershipService;
protected static ActionResult result;
protected static LogOnModel model;
Establish context =() =>
{
var controllerBuilder = new TestControllerBuilder();
formsService = MockRepository.GenerateStub<IFormsAuthenticationService>();
membershipService = MockRepository.GenerateStub<IMembershipService>();
model = MockRepository.GenerateStub<LogOnModel>();
controller =
controllerBuilder.CreateController<AccountController>(new object[]
{
formsService,
membershipService
});
};
Because user_logs =() =>
{
bool rememberMe = false;
membershipService.Stub(
x => x.ValidateUser("bdd", "mspec")).Return(true);
formsService.Stub(x => x.SignIn("bdd", rememberMe));
controller.ModelState.IsValid.ShouldBeTrue();
};
}
[Subject(typeof(AccountController), "LogInTests")]
public class When_logging_into_application_with_good_login_and_password : AccountControllerTests3
{
private It user_should_be_redirected_to_the_home_page =() =>
{
model.UserName = "bdd";
model.Password = "mspec";
result = controller.LogOn(model, string.Empty);
result.AssertActionRedirect().ToAction<HomeController>(
x => x.Index());
};
}
[Subject(typeof(AccountController), "LogInTests")]
public class When_logging_into_application_with_bad_login_and_password : AccountControllerTests3
{
It The_error_message_should_be_shown =() =>
{
model.UserName = "BAD";
model.Password = "BAD";
result = controller.LogOn(model, string.Empty);
controller.ModelState[""].Errors[0].ErrorMessage.ShouldEqual(
"The user name or password provided is incorrect.");
};
}
감사합니다,
토마스
답장을 보내 주셔서 감사합니다. 저는 회선 컨트롤러를 제거 할 수 있다는 것에 동의합니다 .ModelState.IsValid.ShouldBeTrue(); 기본 클래스를 피하는 것이 좋습니다. 그래서 내가 두어야 할 곳은 두 개의 클래스로 나뉘어져 있니? 감사합니다. –
기본 클래스가 없으면 서로 다른 두 가지 컨텍스트가 있습니다. 그래서 예, 두 가지 다른 종결 조항과 서로 다른 두 개의 확립 컨텍스트 조항이 있습니다. –
좋아요, 그렇다면 위임 설정에서했던 일반적인 구성 코드를 어디에 정의해야합니까? –