NUinit CollectionAssert not working as expected
I have a view model class as follows:
public class MyViewModel
{
// Other properties......
public IEnumerable<SelectListItem> SecurityQuestions { get; set; }
}
In my controller I have the following code:
public ViewResult 开发者_JS百科Index()
{
var viewModel = new MyViewModel {SecurityQuestions = GetSecurityQuestions()};
return View(viewModel);
}
public IEnumerable<SelectListItem> GetSecurityQuestions()
{
return new SelectList(_securityQuestionService.GetAll(),
"SecurityQuestionID",
"Question");
}
I have written a unit test to test the Index action method:
[Test]
public void Can_Load_View_With_Security_Questions()
{
var result = _controller.Index();
var questions = _controller.GetSecurityQuestions();
var viewModel = result.ViewData.Model as MyViewModel;
CollectionAssert.AreEqual(questions, viewModel.SecurityQuestions);
}
I was expecting this test to pass without any problems. But I am getting the below error:
Expected: <System.Web.Mvc.SelectList>
But was: <System.Web.Mvc.SelectList>
Why is this happening?
I was expecting this test to pass without any problems
Why would you be expecting such a thing? The problem might come from the fact that you are calling the _securityQuestionService.GetAll()
method twice. The first time inside the controller action and the second time inside your unit test which could different instances. Also the SelectList
type doesn't override the Equals method so what you are experiencing is normal behavior.
精彩评论