佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1296|回复: 2

关于 Unit Test 和 TDD

[复制链接]
发表于 10-10-2013 07:35 AM | 显示全部楼层 |阅读模式
对这个有些疑问;
读了些 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 也要先写了咯?

回复

使用道具 举报


ADVERTISEMENT

发表于 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
  1. [TestMethod]
  2. [ExpectedException(typeof(System.ArgumentOutOfRangeException))]
  3. public void Pass_Through_Variable_Discounts() {
  4. // arrange
  5. Mock<IDiscountHelper> mock = new Mock<IDiscountHelper>();
  6. mock.Setup(m => m.ApplyDiscount(It.IsAny<decimal>()))
  7. .Returns<decimal>(total => total);
  8. mock.Setup(m => m.ApplyDiscount(It.Is<decimal>(v => v == 0)))
  9. .Throws<System.ArgumentOutOfRangeException>();
  10. mock.Setup(m => m.ApplyDiscount(It.Is<decimal>(v => v > 100)))
  11. .Returns<decimal>(total => (total * 0.9M));
  12. mock.Setup(m => m.ApplyDiscount(It.IsInRange<decimal>(10, 100,
  13. Range.Inclusive))).Returns<decimal>(total => total - 5);
  14. var target = new LinqValueCalculator(mock.Object);
  15. // act
  16. decimal FiveDollarDiscount = target.ValueProducts(createProduct(5));
  17. decimal TenDollarDiscount = target.ValueProducts(createProduct(10));
  18. decimal FiftyDollarDiscount = target.ValueProducts(createProduct(50));
  19. decimal HundredDollarDiscount = target.ValueProducts(createProduct(100));
  20. decimal FiveHundredDollarDiscount = target.ValueProducts(createProduct(500));
  21. // assert
  22. Assert.AreEqual(5, FiveDollarDiscount, "$5 Fail");
  23. Assert.AreEqual(5, TenDollarDiscount, "$10 Fail");
  24. Assert.AreEqual(45, FiftyDollarDiscount, "$50 Fail");
  25. Assert.AreEqual(95, HundredDollarDiscount, "$100 Fail");
  26. Assert.AreEqual(450, FiveHundredDollarDiscount, "$500 Fail");
  27. target.ValueProducts(createProduct(0));
  28. }
复制代码
回复

使用道具 举报

发表于 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
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 10-5-2024 04:13 PM , Processed in 0.068892 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表