我想我不明白Mock中有什么东西,如果我使用DynamicMock,它应该只验证我期望的呼叫吗?为什么我在下面的测试中遇到异常?我不想验证会话是否已设置,我只想验证_authService.EmailIsUnique是否使用正确的参数调用。我的问题是,在我的AdminController中,我设置了_authService.Session ...
private MockRepository _mock;
private ISession _session;
private IAuthenticationService _authService;
private AdminController _controller;
private TestControllerBuilder _builder;
[SetUp]
public void
Setup()
{
_mock = new MockRepository();
_session = _mock.DynamicMock<ISession>();
_authService = _mock.DynamicMock<IAuthenticationService>();
_controller = new AdminController(_authService, _session);
_builder = new TestControllerBuilder();
_builder.InitializeController(_controller);
}
[Test]
public void Register_Post_AddModelErrorWhenEmailNotUnique()
{
var userInfo = new RegisterModel();
userInfo.Email = "the_email@domain.com";
//_authService.Expect(x => x.Session = _session).Repeat.Once();
Expect.Call(_authService.EmailIsUnique(userInfo.Email)).Repeat.Once().Return(false);
_mock.ReplayAll();
var result = _controller.Register(userInfo);
var viewResult = (ViewResult)result;
_authService.VerifyAllExpectations();
result.AssertViewRendered().ForView("").WithViewData<RegisterModel>();
Assert.That(viewResult.ViewData.Model, Is.EqualTo(userInfo));
}
Rhino.Mocks.Exceptions.ExpectationViolationException:IAuthenticationService.set_Session(ISessionProxy2f2f623898f34cbeacf2385bc9ec641f);预期#1,实际#0。
感谢您的帮助!
更新
这是我的控制器和我的AuthService的一部分...我正在使用Ninject作为DI,因为我的AuthService在我的域中并且Ninject在我的WebApp中我不知道如何在我的AuthService中使用DI来解决我的会议。再次感谢!
public partial class AdminController : Controller
{
private IAuthenticationService _authService;
private ISession _session;
public AdminController(IAuthenticationService authService, ISession session)
{
_authService = authService;
_authService.Session = session;
_session = session;
}
[HttpPost]
[Authorize(Roles = "Admin")]
[ValidateAntiForgeryToken]
public virtual ActionResult Register(RegisterModel userInfo)
{
if (!_authService.EmailIsUnique(userInfo.Email)) ModelState.AddModelError("Email", Strings.EmailMustBeUnique);
if (ModelState.IsValid)
{
return RegisterUser(userInfo);
}
return View(userInfo);
}
private RedirectToRouteResult RegisterUser(RegisterModel userInfo)
{
_authService.RegisterAdmin(userInfo.Email, userInfo.Password);
var authToken = _authService.ForceLogin(userInfo.Email);
SetAuthCookie(userInfo.Email, authToken);
return RedirectToAction(MVC.Auction.Index());
}
}
public class AuthenticationService : IAuthenticationService
{
public ISession Session { get; set; }
public bool EmailIsUnique(string email)
{
var user = Session.Single<User>(u => u.Email == email);
return user == null;
}
}
答案 0 :(得分:2)
我的问题是在我的AdminController中设置_authService.Session
是的,这确实是一个问题,因为这是DI框架的责任,而不是您的控制器代码。因此,除非控制器直接使用ISession
,否则应将其从中移除。控制器不应该在服务和该服务的任何依赖关系之间进行任何管道连接。
所以这是一个例子:
public class AdminController : Controller
{
private readonly IAuthenticationService _authService;
public AdminController(IAuthenticationService authService)
{
_authService = authService;
}
public ActionResult Register(RegisterModel userInfo)
{
if (!_authService.EmailIsUnique(userInfo.Email))
{
ModelState.AddModelError("Email", Strings.EmailMustBeUnique);
return View(userInfo);
}
return RedirectToAction("Success");
}
}
请注意,管理员控制器不应该依赖任何会话。它已经依赖于IAuthenticationService
。实施此服务的方式并不重要。在这种情况下,适当的单元测试将是:
private IAuthenticationService _authService;
private AdminController _controller;
private TestControllerBuilder _builder;
Setup()
{
_authService = MockRepository.GenerateStub<IAuthenticationService>();
_controller = new AdminController(_authService);
_builder = new TestControllerBuilder();
_builder.InitializeController(_controller);
}
[Test]
public void Register_Post_AddModelErrorWhenEmailNotUnique()
{
// arrange
var userInfo = new RegisterModel();
userInfo.Email = "the_email@domain.com";
_authService
.Stub(x => x.EmailIsUnique(userInfo.Email))
.Return(false);
// act
var actual = _controller.Register(userInfo);
// assert
actual
.AssertViewRendered()
.WithViewData<RegisterModel>()
.ShouldEqual(userInfo, "");
Assert.IsFalse(_controller.ModelState.IsValid);
}
更新:
现在你已经展示了你的代码我确认了:
从控制器中删除ISession依赖项,因为它不需要,并将此作业留给DI框架。
现在我可以看到你的AuthenticationService对ISession有很强的依赖性,所以构造函数注入会更适应性质注入。仅对可选依赖项使用属性注入:
public class AuthenticationService : IAuthenticationService
{
private readonly ISession _session;
public AuthenticationService(ISession session)
{
_session = session;
}
public bool EmailIsUnique(string email)
{
var user = _session.Single<User>(u => u.Email == email);
return user == null;
}
}
,剩下的最后一部分就是管道。这是在ASP.NET MVC应用程序中完成的,该应用程序引用了所有其他层。因为你提到了Ninject,你可以安装Ninject.MVC3 NuGet并在生成的~/App_Start/NinjectMVC3
中简单地配置内核:
/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<ISession>().To<SessionImpl>();
kernel.Bind<IAuthenticationService>().To<AuthenticationService>();
}