查看: 1296|回复: 2
|
关于 Unit Test 和 TDD
[复制链接]
|
|
对这个有些疑问;
读了些 article
有几点不明白:
Unit Test 应该是 Isolated 的,那么 CRUD Test Case 应如何写?需要跟着次序??还是只是Pass Data, 成功就 Success, Exception 就 Fail。那么 REad/Get呢?
Mock 和 Stub..不太明白它们的用途和分别。Mock 与 不Mock又怎样?
TDD.. Test Driven Development: 这个...难道是说还没Start Development 就先写完 Unit Test, 过后不能改 Unit Test??
这样的话,整个 Project 的 Class 和 Structure 也要先写了咯?
|
|
|
|
|
|
|
|
发表于 10-10-2013 02:40 PM
|
显示全部楼层
Unit Test 应该是 Isolated 的,那么 CRUD Test Case 应如何写?需要跟着次序??还是只是Pass Data, 成功就 Success, Exception 就 Fail。那么 REad/Get呢?
Mock 和 Stub..不太明白它们的用途和分别。Mock 与 不Mock又怎样?
没错的话:
Mock: prepare fake data for functionality test
One of the reasons that we were able to keep our tests so simple in the previous section was because we
were testing a single class that depended on no other class to function. Such objects exist in real projects,
of course, but you will also need to test objects that cannot function in isolation. In these situations, you
need to be able to focus on the class or method you are interested in, so that you are not implicitly testing
the classes that are depended on as well.
One useful approach is to use mock objects, which simulator the functionality of real objects from
your project, but in a very specific and controlled way. Mock objects let you narrow the focus of your tests
so that you only examine the functionality you are interested in.
The paid-for versions of Visual Studio 2012 include support for creating mock objects through a
feature called fakes, but we prefer to use a library called Moq, which is simple, easy-to-use and can be
used with all Visual Studio editions, including the free ones.
strategy:
arrange>act>assert- [TestMethod]
- [ExpectedException(typeof(System.ArgumentOutOfRangeException))]
- public void Pass_Through_Variable_Discounts() {
- // arrange
- Mock<IDiscountHelper> mock = new Mock<IDiscountHelper>();
- mock.Setup(m => m.ApplyDiscount(It.IsAny<decimal>()))
- .Returns<decimal>(total => total);
- mock.Setup(m => m.ApplyDiscount(It.Is<decimal>(v => v == 0)))
- .Throws<System.ArgumentOutOfRangeException>();
- mock.Setup(m => m.ApplyDiscount(It.Is<decimal>(v => v > 100)))
- .Returns<decimal>(total => (total * 0.9M));
- mock.Setup(m => m.ApplyDiscount(It.IsInRange<decimal>(10, 100,
- Range.Inclusive))).Returns<decimal>(total => total - 5);
- var target = new LinqValueCalculator(mock.Object);
- // act
- decimal FiveDollarDiscount = target.ValueProducts(createProduct(5));
- decimal TenDollarDiscount = target.ValueProducts(createProduct(10));
- decimal FiftyDollarDiscount = target.ValueProducts(createProduct(50));
- decimal HundredDollarDiscount = target.ValueProducts(createProduct(100));
- decimal FiveHundredDollarDiscount = target.ValueProducts(createProduct(500));
- // assert
- Assert.AreEqual(5, FiveDollarDiscount, "$5 Fail");
- Assert.AreEqual(5, TenDollarDiscount, "$10 Fail");
- Assert.AreEqual(45, FiftyDollarDiscount, "$50 Fail");
- Assert.AreEqual(95, HundredDollarDiscount, "$100 Fail");
- Assert.AreEqual(450, FiveHundredDollarDiscount, "$500 Fail");
- target.ValueProducts(createProduct(0));
- }
复制代码 |
|
|
|
|
|
|
|
发表于 16-8-2014 04:34 PM
|
显示全部楼层
TDD.. Test Driven Development
是先写TEST CODE才写source code这是为了让你的program集中在一个function还有方便debug因为这有一个function 这是为什么它叫UNIT TEST。 如果你的function pass完你写的test code才去下一个function |
|
|
|
|
|
|
| |
本周最热论坛帖子
|