0

I'm trying to unit test my ASP MVC application, but my test fails with the message Message: Assert.AreEqua failed. Expected:<>. Actual: <Error>.

Now, I assume this is because I've set up the test wrong, but I can't find the place where I might have missed something. Any hints would be greatly appreciated. I'm using Moq.

My test class:

[TestClass] public class ErrorControllerTest { [TestMethod] public void TestErrorView() { var repositoryMock = new Mock<IErrorRepository>(); var errors = new List<ErrorModel>(); errors.Add(new ErrorModel() { Id = "id", Message = "message" }); repositoryMock.Setup(r => r.GetErrors()).Returns(errors); var controller = new ErrorController(repositoryMock.Object); var result = (ViewResult) controller.Error(1); Assert.AreEqual(result.ViewName, "Error"); repositoryMock.VerifyAll(); } } 

For good measure, this is the controller under test:

public class ErrorController : Controller { private readonly IErrorRepository errorRepository; public ErrorController(IErrorRepository errorRepository) { this.errorRepository = errorRepository; } public ActionResult Error(int? page) { var errors = errorRepository.GetErrors(); //// stuff for paging int pageSize = 10; int pageNumber = (page ?? 1); // if there is no page, return page 1 return View(errors.ToPagedList(pageNumber, pageSize)); } } 

And the repository:

 public interface IErrorRepository { List<ErrorModel> GetErrors(); } public class ErrorRepository : IErrorRepository { public ErrorModel Errors { get; set; } public List<ErrorModel> ErrorList { get; set; } public List<ErrorModel> GetErrors() { string cs = "Data Source=" + "some path"; using (SQLiteConnection con = new SQLiteConnection(cs)) { var listOfErrors = new List<ErrorModel>(); string stm = "SELECT * FROM Error"; con.Open(); using (SQLiteCommand cmd = new SQLiteCommand(stm, con)) { using (SQLiteDataReader rdr = cmd.ExecuteReader()) { while (rdr.Read()) { listOfErrors.Add(new ErrorModel { Id = rdr["ID"].ToString(), Message = rdr["Message"].ToString() }); } rdr.Close(); ErrorList = listOfErrors; } } con.Close(); } return ErrorList; } } 
1
  • 1
    If this Assert.AreEqual(result.ViewName, "Error"); gives Expected:<>. Actual: <Error> then you have your arguments round the wrong way. Every test environment does it differently (within the 2 options obviously) - check intellisense, but it should probably be Assert.AreEqual("Error", result.ViewName);. Doesn't explain why it's blank though. Commented Oct 8, 2015 at 8:04

2 Answers 2

1

I don't know what is the result you are getting from this result.ViewName but it should be equal to the text "Error" to make your test pass. Otherwise you should change that "Error" to the actual result and result.ViewName to expected result.

Assert.AreEqual(result.ViewName, "Error"); 

Remember this: Assert.AreEqual(expected result, actual result);

If you are still confuse see my sample code below to let you see a clearer view.

This is my expected result: http://prntscr.com/8p0r03

and this is my actual result: http://prntscr.com/8p0qnj

Sign up to request clarification or add additional context in comments.

1 Comment

Ah yes, I fully understood your answer :). I'm just flabbergasted why my test doesn't pass. When I just run the application, I get the expected view, but I don't know why my unit test doesn't get it.
1

When using Assert.AreEqual() you should always put the expected value first, then the actual value, otherwise your results can get confusing. What your test means to say is that result.ViewName is empty, when it expected it to be "Error". Your configuration is correct (apart from that little niggle), and it's highlighting either a) a bug in your code somewhere, or b) your belief that result.ViewName should be "Error" is not correct.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.