Я новичок в TDD и RhinoMocks.RhinoMocks AssertWasCalled throws Exception
Я пытаюсь проверить AssertWasCalled, но с проблемами. Конструктор для моего теста выглядит следующим образом:
public AccountControllerTests()
{
_webAuthenticator = MockRepository.GenerateMock<IWebAuthenticator>();
}
И мой тест, как это:
[TestMethod]
public void AccountControllerCallsWebAuthenticator_CreateSignInTicketForGoodLoginCredentials()
{
const string username = "good-username";
const string password = "good-password";
var model = new LoginModel { Username = username, Password = password };
_webAuthenticator.Stub(w => w.Authenticate(username, password)).Return(true);
var mockHttpContextBase = MockRepository.GenerateMock<HttpContextBase>();
var accountController = new AccountController(_webAuthenticator);
accountController.Login(model);
_webAuthenticator.AssertWasCalled(x => x.CreateSignInTicket(mockHttpContextBase, username));
}
ошибка я получаю:
Метод испытания Paxium.Music.WebUI.Tests. Controllers.AccountControllerTests.AccountControllerCallsWebAuthenticator_CreateSignInTicketForGoodLoginCredentials сделал исключение: Rhino.Mocks.Exceptions.ExpectationViolationException: IWebAuthenticator.CreateSignInTicket (Castle.Proxies.HttpContextBaseProxy7f274f09b6124e6da32d96dc6d3fface, "go спосо-имя пользователя "); Ожидаемое # 1, действительное # 0.
Теперь я изменил код, как показано ниже - до и после кода:
До:
public class AccountController : Controller
{
private readonly IWebAuthenticator _webAuthenticator;
public AccountController(IWebAuthenticator webAuthenticator)
{
_webAuthenticator = webAuthenticator;
}
[HttpGet]
public ActionResult Login()
{
return View();
}
[HttpPost]
public ActionResult Login(LoginModel model)
{
if (ModelState.IsValid)
{
if (_webAuthenticator.Authenticate(model.Username, model.Password))
{
_webAuthenticator.CreateSignInTicket(HttpContext, model.Username);
return RedirectToAction("Index", "Home");
}
return View(model);
}
return View(model);
}
}
После:
public class AccountController : Controller
{
private readonly IWebAuthenticator _webAuthenticator;
private readonly HttpContextBase _contextBase;
public AccountController()
{
}
public AccountController(IWebAuthenticator webAuthenticator, HttpContextBase contextBase)
{
_webAuthenticator = webAuthenticator;
_contextBase = contextBase;
}
[HttpGet]
public ActionResult Login()
{
return View();
}
[HttpPost]
public ActionResult Login(LoginModel model)
{
if (ModelState.IsValid)
{
if (_webAuthenticator.Authenticate(model.Username, model.Password))
{
_webAuthenticator.CreateSignInTicket(_contextBase, model.Username);
return RedirectToAction("Index", "Home");
}
return View(model);
}
return View(model);
}
}
Мои тесты в настоящее время проходят. Как я вставляю в contextBase, хотя когда мой контроллер используется для реального? Я использую StructureMap.
Я изменил код, чтобы придать контекстную базу, но все еще немного застрял - добавил примечания к моему первоначальному вопросу. Надеюсь, это правильный способ сделать что-то? –
Хорошо, я понимаю, что инъекция HttpContextBase неверна, поэтому я думаю, что теперь понимаю, что у меня менее строгое утверждение. Я получил он за работу: '_webAuthenticator.AssertWasCalled (х => x.CreateSignInTicket (Arg .Is.Anything, Arg .Is.Anything));' Однако следующий говорит: «Не удается получить доступ к не статический метод 'Равно' в статическом контексте» ' _webAuthenticator.AssertWasCalled (х => x.CreateSignInTicket (Arg .Is.Anything, Arg .equals (имя пользователя))); ' –
My плохо, правильный синтаксис - это Arg. Is.Equal (...) или ярлык Arg (...) –
prgmtc