测试驱动开发上的五大错误

发表于:2013-3-15 10:38

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:未知    来源:51Testing软件测试网采编

分享:

  你也许还会质疑这些模拟代码中的这个没有任何用处的 GetProducts()方法,它是干什么的?因为IProductRepository接口里有这个方法,我们不得不加入这个方法以让程序能编译通过——尽管在我们的测试中这个方法根本不是我们考虑到对象。

  使用这样的测试方法,我们不得不写出大量的临时模拟类,这无疑会让我们在维护时愈加头痛。这种时候,使用一个模拟框架,比如JustMock,将会节省我们大量的工作。

  让我们重新看一下之前的这个测试例子,这次我们将使用一个模拟框架:

  1. [TestMethod]  
  2. public void GetProductWithValidIDReturnsProduct()  
  3. {  
  4.     // Arrange  
  5.     IProductRepository productRepository = Mock.Create<IProductRepository>();  
  6.     Mock.Arrange(() => productRepository.GetByID("spr-product")).Returns(new Product());  
  7.     ProductService productService = new ProductService(productRepository);  
  8.      
  9.     // Act  
  10.     Product product = productService.GetByID("spr-product");  
  11.      
  12.     // Assert  
  13.     Assert.IsNotNull(product);  
  14. }  
  15.      
  16. [TestMethod]  
  17. public void GetProductWithInValidIDThrowsException()  
  18. {  
  19.     // Arrange  
  20.     IProductRepository productRepository = Mock.Create<IProductRepository>();  
  21.     ProductService productService = new ProductService(productRepository);  
  22.      
  23.     // Act & Assert  
  24.     Assert.Throws<ProductNotFoundException>(() => productService.GetByID("invalid-id"));  
  25. }

  有没有注意到我们写的代码的减少量?在这个例子中代码量减少49%,更准确的说,使用模拟框架测试时代码是28行,而没有使用时是57行。我们还看到了整个测试方法变得可读性更强了!

  2、测试代码组织的太松散

  模拟框架让我们在模拟测试中的生成某个依赖类的工作变得非常简单,但有时候太轻易实现也容易产生坏处。为了说明这个观点,请观察下面两个单元测试,看看那一个容易理解。这两个测试程序是测试一个相同的功能:

  Test #1

  1. TestMethod]  
  2. public void InitializeWithValidProductIDReturnsView()  
  3. {  
  4.     // Arrange  
  5.     IProductView productView = Mock.Create<IProductView>();  
  6.     Mock.Arrange(() => productView.ProductID).Returns("spr-product");  
  7.      
  8.     IProductService productService = Mock.Create<IProductService>();  
  9.     Mock.Arrange(() => productService.GetByID("spr-product")).Returns(new Product()).OccursOnce();  
  10.      
  11.     INavigationService navigationService = Mock.Create<INavigationService>();  
  12.     Mock.Arrange(() => navigationService.GoTo("/not-found"));  
  13.      
  14.     IBasketService basketService = Mock.Create<IBasketService>();  
  15.     Mock.Arrange(() => basketService.ProductExists("spr-product")).Returns(true);  
  16.          
  17.     var productPresenter = new ProductPresenter(  
  18.                                             productView,  
  19.                                             navigationService,  
  20.                                             productService,   
  21.                                             basketService);  
  22.      
  23.     // Act  
  24.     productPresenter.Initialize();  
  25.      
  26.     // Assert  
  27.     Assert.IsNotNull(productView.Product);  
  28.     Assert.IsTrue(productView.IsInBasket);  
  29. }

  Test #2

  1. [TestMethod]  
  2. public void InitializeWithValidProductIDReturnsView()  
  3. {  
  4.     // Arrange     
  5.     var view = Mock.Create<IProductView>();  
  6.     Mock.Arrange(() => view.ProductID).Returns("spr-product");  
  7.      
  8.     var mock = new MockProductPresenter(view);  
  9.      
  10.     // Act  
  11.     mock.Presenter.Initialize();  
  12.      
  13.     // Assert  
  14.     Assert.IsNotNull(mock.Presenter.View.Product);  
  15.     Assert.IsTrue(mock.Presenter.View.IsInBasket);  
  16. }

63/6<123456>
2023测试行业从业人员调查问卷已开启,千元大奖正在等你~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2023
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号