Reputation: 2765
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;
}
}
Upvotes: 0
Views: 205
Reputation: 4911
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
Upvotes: 1
Reputation: 956
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.
Upvotes: 1