So I've been writing a unit test using Rhino Mocks to a controller in my ASP MVC application. The test passes, but I'm interested in your opinion and if there's something I should be doing differently or what it could be.
Here's my test method:
[TestMethod]
public void TestSlideView()
{
// Arrange
var repositoryMock = MockRepository.GenerateMock<ISlideRepository>();
var controller = new SlideController(repositoryMock);
var expectedSlides = new List<SlideModel>();
expectedSlides.Add(new SlideModel
{
Id = "id"
});
repositoryMock.Stub(x => x.GetSlides("id")).Return(expectedSlides);
// Act
var actualView = controller.Slide("id") as ViewResult;
var actualData = actualView.Model;
// Assert
Assert.IsNotNull(actualView);
Assert.IsNotNull(actualData);
Assert.AreEqual("Slide", actualView.ViewName);
}
Here's the controller under test:
public ActionResult Slide(string slideid)
{
var slides = slideRepository.GetSlides(slideid);
return View("Slide", slides);
}
The repository the controller uses is here:
public interface ISlideRepository
{
List<SlideModel> GetSlides(string id);
}
public class SlideRepository : ISlideRepository
{
public SlideModel Slides { get; set; }
public List<SlideModel> SlideList { get; set; }
public List<SlideModel> GetSlides(string id)
{
string cs = dbPath;
string slideid = id;
using (SQLiteConnection con = new SQLiteConnection(cs))
{
var listOfSlides = new List<SlideModel>();
string stm = "SELECT * FROM Slide WHERE ID = " + slideid;
con.Open();
using (SQLiteCommand cmd = new SQLiteCommand(stm, con))
{
using (SQLiteDataReader rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
listOfSlides.Add(new SlideModel
{
Id = rdr["ID"].ToString()
});
}
rdr.Close();
SlideList = listOfSlides;
}
}
con.Close();
}
return SlideList;
}
}
Any input is greatly appreciated!