Tag: unit testing

AngularJS控制器unit testing – 注入服务

一个关于AngularJSunit testing的希望简单的问题。 我有一个控制器使用一个简单的服务(改编自angular种子项目) services.js: angular.module('myApp.services', []).value('version', '0.1'); controllers.js: function MyCtrl1($s, version) { $s.version = version; } MyCtrl1.$inject = ["$scope","version"]; 这工作很好,我的应用程序。 但是,在unit testing框架工作中,我无法创build控制器。 我不知道如何注入'版本'服务(或创build实例),并将其传递给$ controller()工厂 – 我想这就是我想要做的?! 这是裸骨的规格: controllerSpec.js : beforeEach(inject(function($rootScope, $controller) { scope = $rootScope.$new(); // how about version service? ctrl = $controller(MyCtrl1, {$scope: scope, /* version: <where from?> */}); })); it('Version should be 0.1 […]

Android Studio中的Android Instrumentationtesting和unit testing之间的区别?

从Android Studio 1.1rc开始,有unit testing支持 ,我想知道Android Instrumentationtesting和unit testing有什么区别。 据我所知: unit testing对于testing不会调用Android API的代码非常有用,而Androidtestingtesting则是用于testingAndroid API特定元素或GUI组件的集成testing。 但是,如果你在unit testing中使用Robolectric或Mockito这样的框架,如果我没有弄错,你可以testingAndroid代码(不需要设备)。 这是正确的,还是有更大的区别? 如果是这样,每个的用途是什么?

你可以添加一个自定义的消息AssertJ assertThat?

我们有一个testing套件,主要使用与Hamcrest匹配器的JUnit断言。 我们的一个团队开始尝试AssertJ ,并以其语法,灵活性和声明性给人印象深刻。 JUnit提供了一个function,我无法在AssertJ中find相应的function:添加自定义断言失败消息。 我们经常比较不是为了人类可读性而devise的对象,并且会有随机的Ids或UUID,而且不可能通过它们包含的数据来判断它们应该是什么。 遗憾的是,这对于我们的代码库是不可避免的,因为它实现的目标之一就是在其他服务之间映射数据,而不必理解它是什么。 在JUnit中, assertThat方法在Matcher<T>参数之前提供了一个带有String reason参数的版本。 这使得添加一个简短的debugging串来解决这个问题变得微不足道,比如说比较对于一个人来说意味着什么。 另一方面,AssertJ提供了一个不同的通用static assertThat 断言 ,这个方法返回某种forms的接口Assert或其中一个实现类。 此接口不提供设置自定义消息的标准方式,以包含故障。 有没有办法从AssertJ API或其扩展之一获得这个function,而不必为我们想要添加消息的每个断言types创build一个自定义的断言类 ?

在大多数JUnittesting中抛出Exception是否违背最佳实践?

几乎所有的JUnittesting都是用以下签名书写的: public void testSomething() throws Exception 我的推理是我可以专注于我正在testing的内容,而不是JUnit似乎给我免费的exception处理。 但是我这样做是否错过了什么? 这是否违背最佳做法? 我会通过在testing中明确捕获特定的exception,然后失败()来获得任何东西吗?

PHPUnit模拟对象和方法types提示

我正在尝试使用PHPunit创build\ SplObserver的模拟对象,并将模拟对象附加到\ SplSubject。 当我尝试将模拟对象附加到实现了\ SplSubject的类时,我得到一个可捕获的致命错误,指出模拟对象没有实现\ SplObserver: PHP Catchable fatal error: Argument 1 passed to ..\AbstractSubject::attach() must implement interface SplObserver, instance of PHPUnit_Framework_MockObject_Builder_InvocationMocker given, called in ../Decorator/ResultCacheTest.php on line 44 and defined in /users/…/AbstractSubject.php on line 49 或多或less,这是代码: // Edit: Using the fully qualified name doesn't work either $observer = $this->getMock('SplObserver', array('update')) ->expects($this->once()) ->method('update'); // Attach […]

用moq嘲笑虚拟只读属性

我找不到办法做到这一点,虽然这可以手工完成,为什么不与moq?

如何unit testing返回JsonResult的Action方法?

如果我有这样的控制器: [HttpPost] public JsonResult FindStuff(string query) { var results = _repo.GetStuff(query); var jsonResult = results.Select(x => new { id = x.Id, name = x.Foo, type = x.Bar }).ToList(); return Json(jsonResult); } 基本上,我从我的仓库中获取东西,然后将其投影到匿名types的List<T>中。 我如何进行unit testing? System.Web.Mvc.JsonResult有一个名为Data的属性,但是它的types是object ,正如我们所期望的那样。 那么这是否意味着如果我想testingJSON对象具有我期望的属性(“id”,“name”,“type”),我必须使用reflection? 编辑: 这是我的testing: // Arrange. const string autoCompleteQuery = "soho"; // Act. var actionResult = _controller.FindLocations(autoCompleteQuery); // Assert. Assert.IsNotNull(actionResult, […]

python,unittest:有没有办法将命令行选项传递给应用程序

我有一个模块,导入unittest和有一些TestCases。 我想接受一些命令行选项(例如下面的数据文件的名称),但是当我尝试传递选项时,我收到消息“选项-i not recognized”。 是否有可能unit testing+提供选项的应用程序(注:我使用optparse来处理选项)? 谢谢。 $ python test_app_data.py -i data_1.txt option -i not recognized ===================== 后续:这是一个build议解决scheme的实现: import cfg_master #has the optparse option-handling code … if __name__ == '__main__': #add you app's options here… options_tpl = ('-i', '–in_dir', '-o', '–out_dir') del_lst = [] for i,option in enumerate(sys.argv): if option in options_tpl: del_lst.append(i) del_lst.append(i+1) del_lst.reverse() […]

.NET 4.0代码合同 – 它们将如何影响unit testing?

比如这篇文章介绍了他们。 有什么好处? 静态分析看起来很酷,但同时也会阻止在unit testing中将null作为parameter passing的能力。 (如果你是在文章中的例子) 关于unit testing这个话题,如果你现在已经开始进行自动化testing了,现在肯定是没有意义的。 更新 玩过代码合同,我有点失望。 例如,根据接受的答案中的代码: public double CalculateTotal(Order order) { Contract.Requires(order != null); Contract.Ensures(Contract.Result<double>() >= 0); return 2.0; } 对于unit testing,您仍然需要编写testing以确保不能传递null,如果合同是业务逻辑 ,则结果大于或等于零。 换句话说,如果我要删除第一份合同,除非我专门对这个function进行了testing,否则testing不会中断。 这是基于不使用内置于Visual Studio的更好(最终等)版本的静态分析。 基本上,他们都归结为写传统的if语句的替代方式。 我实际使用TDD的经验,代码合同显示了为什么,以及我如何去做。

模拟或存根链接调用

protected int parseExpire(CacheContext ctx) throws AttributeDefineException { Method targetMethod = ctx.getTargetMethod(); CacheEnable cacheEnable = targetMethod.getAnnotation(CacheEnable.class); ExpireExpr cacheExpire = targetMethod.getAnnotation(ExpireExpr.class); // check for duplicate setting if (cacheEnable.expire() != CacheAttribute.DO_NOT_EXPIRE && cacheExpire != null) { throw new AttributeDefineException("expire are defined both in @CacheEnable and @ExpireExpr"); } // expire time defined in @CacheEnable or @ExpireExpr return cacheEnable.expire() != […]