Recently I have been trying to improve my unit tests and one of the 'rules' of UT that really confuses me is the 'one assert per test'.
I am interested to know whether people think MS have done the right thing in regards to asserting this test (ignore the lack of mocks, etc). Based on my current understanding, this example should really be performing one creation calls per object property that needs to be tested (instead of one call and multiple asserts). Am I correct in making this assumption?
Method taken from: http://msdn.microsoft.com/en-us/vs2010trainingcourse_aspnetmvc3testing_topic4
[TestMethod()]
[DeploymentItem("MvcMusicStore.mdf")]
[DeploymentItem("MvcMusicStore_log.ldf")]
public void CreateTest()
{
using (TransactionScope ts = new TransactionScope())
{
StoreManagerController target = new StoreManagerController();
Album album = new Album()
{
GenreId = 1,
ArtistId = 1,
Title = "New Album",
Price = 10,
AlbumArtUrl = "/Content/Images/placeholder.gif"
};
ActionResult actual;
actual = target.Create(album);
Assert.IsTrue(album.AlbumId != 0);
MusicStoreEntities storeDB = new MusicStoreEntities();
var newAlbum = storeDB.Albums.SingleOrDefault(a => a.AlbumId == album.AlbumId);
Assert.AreEqual(album.GenreId, newAlbum.GenreId);
Assert.AreEqual(album.ArtistId, newAlbum.ArtistId);
Assert.AreEqual(album.Title, newAlbum.Title);
Assert.AreEqual(album.Price, newAlbum.Price);
Assert.AreEqual(album.AlbumArtUrl, newAlbum.AlbumArtUrl);
}
}
By version would be something like (replicated for each property on the album object)
[TestMethod()]
public void CreateTest_AlbumUrl()
{
// ** Arrange
var storeDB = new Mock<MusicStoreEntities>()
// Some code to setup the mocked store would go here
StoreManagerController target = new StoreManagerController(storeDB);
Album album = new Album()
{
GenreId = 1,
ArtistId = 1,
Title = "New Album",
Price = 10,
AlbumArtUrl = "/Content/Images/placeholder.gif"
};
// ** Act
actual = target.Create(album);
var newAlbum = storeDB.Albums.SingleOrDefault(a => a.AlbumId == album.AlbumId);
// ** Assert
Assert.AreEqual(album.AlbumArtUrl, newAlbum.AlbumArtUrl);
}