最近、私は単体テストを改善しようとしていますが、UT の「ルール」の 1 つで、本当に混乱しているのは「テストごとに 1 つのアサート」です。
このテストのアサートに関して、MS が正しいことをしたと人々が考えているかどうかを知りたいです (モックの欠如などは無視してください)。私の現在の理解に基づくと、この例では、(1 回の呼び出しと複数のアサートではなく) テストする必要があるオブジェクト プロパティごとに 1 回の作成呼び出しを実際に実行する必要があります。この仮定は正しいですか?
以下から取得した方法: 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);
}
}
バージョンごとに次のようになります (アルバム オブジェクトのプロパティごとに複製されます)
[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);
}