在此示例中,Microsoft 对每个测试执行多个断言是否正确? [英] Is Microsoft right to be performing multiple asserts per test in this example?
问题描述
最近我一直在努力改进我的单元测试,真正让我困惑的 UT 的规则"之一是每个测试一个断言".
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'.
我很想知道人们是否认为 MS 在断言此测试方面做了正确的事情(忽略缺乏模拟等).根据我目前的理解,这个例子应该对每个需要测试的对象属性执行一次创建调用(而不是一次调用和多次断言).我做出这个假设是否正确?
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?
方法取自: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);
}
推荐答案
这条规则经常被误解.这不是关于单个断言(如代码行/Assert
调用),而是关于验证单个概念.在这种情况下,Microsoft 会验证是否正确添加了专辑 - 此处专辑是单个概念.
This rule is way too often misunderstood. It's not about single assertion (as in line of code/ Assert
call), but about verifying single concept. In this case, Microsoft verifies album was added correctly - album is single concept here.
我的指导方针通常是每次测试测试一个逻辑概念.你可以在同一个对象上有多个断言.它们通常是被测试的同一个概念.
My guideline is usually that you test one logical CONCEPT per test. you can have multiple asserts on the same object. they will usually be the same concept being tested.
这篇关于在此示例中,Microsoft 对每个测试执行多个断言是否正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!