我有一个node.js项目,其中包含一些Jasmine规范.规范位于spec /子目录中,并且具有.spec.coffee扩展名,如jasmine-node所示.
当我在WebStorm IDE中打开我的一个spec文件时,所有对beforeEachand describe和的调用it都显示为带有工具提示的蓝色波浪形下划线:"未解析的函数或方法it()".因此即使我使用3.0 EAP并且它应该有一定量的Jasmine支持,它也不会自动发现这是一个Jasmine规范文件.
我尝试进入文件>设置> JavaScript库,并添加Jasmine作为库(指定jasmine-2.0.0.rc1.js的路径),然后转到Usage Scope子页面并检查"Jasmine"中的"Project"旁边的下拉列表,但没有效果 - Jasmine方法仍然显示为未解决.
如何告诉WebStorm spec子目录中的所有文件和/或扩展名为.spec.coffee的所有文件都是Jasmine测试,并让它识别那些测试正在使用的Jasmine API?
我有一个函数我想测试哪个调用外部API方法两次,使用不同的参数.我想用Jasmine间谍模拟这个外部API,并根据参数返回不同的东西.在Jasmine有什么办法吗?我能想到的最好的是使用andCallFake的hack:
var functionToTest = function() {
var userName = externalApi.get('abc');
var userId = externalApi.get('123');
};
describe('my fn', function() {
it('gets user name and ID', function() {
spyOn(externalApi, 'get').andCallFake(function(myParam) {
if (myParam == 'abc') {
return 'Jane';
} else if (myParam == '123') {
return 98765;
}
});
});
});
Run Code Online (Sandbox Code Playgroud) 我有一个名为的角度服务requestNotificationChannel:
app.factory("requestNotificationChannel", function($rootScope) {
var _DELETE_MESSAGE_ = "_DELETE_MESSAGE_";
function deleteMessage(id, index) {
$rootScope.$broadcast(_DELETE_MESSAGE_, { id: id, index: index });
};
return {
deleteMessage: deleteMessage
};
});
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用jasmine对此服务进行单元测试:
"use strict";
describe("Request Notification Channel", function() {
var requestNotificationChannel, rootScope, scope;
beforeEach(function(_requestNotificationChannel_) {
module("messageAppModule");
inject(function($injector, _requestNotificationChannel_) {
rootScope = $injector.get("$rootScope");
scope = rootScope.$new();
requestNotificationChannel = _requestNotificationChannel_;
})
spyOn(rootScope, '$broadcast');
});
it("should broadcast delete message notification", function(done) {
requestNotificationChannel.deleteMessage(1, 4);
expect(rootScope.$broadcast).toHaveBeenCalledWith("_DELETE_MESSAGE_", { id: 1, index: 4 });
done();
});
});
Run Code Online (Sandbox Code Playgroud)
我读到了Jasmine中的异步支持,但由于我对使用javascript的单元测试不熟悉,因此无法使其正常工作.
我收到一个错误: …
我有一个templateUrl定义的AngularJS指令.我正在尝试用Jasmine进行单元测试.
根据以下建议,我的Jasmine JavaScript如下所示:
describe('module: my.module', function () {
beforeEach(module('my.module'));
describe('my-directive directive', function () {
var scope, $compile;
beforeEach(inject(function (_$rootScope_, _$compile_, $injector) {
scope = _$rootScope_;
$compile = _$compile_;
$httpBackend = $injector.get('$httpBackend');
$httpBackend.whenGET('path/to/template.html').passThrough();
}));
describe('test', function () {
var element;
beforeEach(function () {
element = $compile(
'<my-directive></my-directive>')(scope);
angular.element(document.body).append(element);
});
afterEach(function () {
element.remove();
});
it('test', function () {
expect(element.html()).toBe('asdf');
});
});
});
});
Run Code Online (Sandbox Code Playgroud)
当我在我的Jasmine规范错误中运行它时,我收到以下错误:
TypeError: Object #<Object> has no method 'passThrough'
Run Code Online (Sandbox Code Playgroud)
我想要的是模板加载原型 - 我不想使用respond.我相信这可能与使用ngMock …
我写了一个AngularJS服务,我想对它进行单元测试.
angular.module('myServiceProvider', ['fooServiceProvider', 'barServiceProvider']).
factory('myService', function ($http, fooService, barService) {
this.something = function() {
// Do something with the injected services
};
return this;
});
Run Code Online (Sandbox Code Playgroud)
我的app.js文件已注册:
angular
.module('myApp', ['fooServiceProvider','barServiceProvider','myServiceProvider']
)
Run Code Online (Sandbox Code Playgroud)
我可以测试DI是这样工作的:
describe("Using the DI framework", function() {
beforeEach(module('fooServiceProvider'));
beforeEach(module('barServiceProvider'));
beforeEach(module('myServiceProvder'));
var service;
beforeEach(inject(function(fooService, barService, myService) {
service=myService;
}));
it("can be instantiated", function() {
expect(service).not.toBeNull();
});
});
Run Code Online (Sandbox Code Playgroud)
这证明了服务可以由DI框架创建,但是接下来我想对服务进行单元测试,这意味着模拟注入的对象.
我该怎么做呢?
我已经尝试将模拟对象放在模块中,例如
beforeEach(module(mockNavigationService));
Run Code Online (Sandbox Code Playgroud)
并将服务定义重写为:
function MyService(http, fooService, barService) {
this.somthing = function() {
// Do something with the injected services
};
});
angular.module('myServiceProvider', …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Jasmine为基本的jQuery AJAX请求编写一些BDD规范.我目前在独立模式下使用Jasmine(即通过SpecRunner.html).我已配置SpecRunner来加载jquery和其他.js文件.任何想法为什么以下不起作用?has_returned并不成真,甚至想到了"yuppi!" 警报显示正常.
describe("A jQuery ajax request should be able to fetch...", function() {
it("an XML file from the filesystem", function() {
$.ajax_get_xml_request = { has_returned : false };
// initiating the AJAX request
$.ajax({ type: "GET", url: "addressbook_files/addressbookxml.xml", dataType: "xml",
success: function(xml) { alert("yuppi!"); $.ajax_get_xml_request.has_returned = true; } });
// waiting for has_returned to become true (timeout: 3s)
waitsFor(function() { $.ajax_get_xml_request.has_returned; }, "the JQuery AJAX GET to return", 3000);
// TODO: other tests might check size of …Run Code Online (Sandbox Code Playgroud) 这两个测试框架之间的主要区别是什么?
我从一开始就是测试驱动开发的新手.
我一直使用Jasmine进行单元测试,但最近我开始使用Istanbul来给我代码覆盖报告.我的意思是我得到他们试图告诉我的要点,但我不知道每个百分比代表什么(Stmts,Branches,Funcs,Lines).到目前为止谷歌搜索我一直无法找到可靠的解释/资源.
问题:就像我说的那样,我得到了它的要点,但有人可以发布正确的解释或链接到正确的解释吗?
第三个问题:有没有办法确定代码的哪些特定部分未被涵盖?到目前为止,我还没有真正地了解这份报告,我基本上在猜测.
-------------------|-----------|-----------|-----------|-----------|
File | % Stmts |% Branches | % Funcs | % Lines |
-------------------|-----------|-----------|-----------|-----------|
controllers/ | 88.1 | 77.78 | 78.57 | 88.1 |
dashboard.js | 88.1 | 77.78 | 78.57 | 88.1 |
-------------------|-----------|-----------|-----------|-----------|
All files | 88.1 | 77.78 | 78.57 | 88.1 |
-------------------|-----------|-----------|-----------|-----------|
Run Code Online (Sandbox Code Playgroud) (这里有一个相关的问题:Jasmine测试没有看到AngularJS模块)
我只是想在没有引导Angular的情况下测试服务.
我看了一些例子和教程,但我不会去任何地方.
我只有三个文件:
myService.js:我在哪里定义AngularJS服务
test_myService.js:我为服务定义了Jasmine测试.
specRunner.html:具有正常jasmine配置的HTML文件,我导入前两个其他文件以及Jasmine,Angularjs和angular-mocks.js.
这是服务的代码(当我没有测试时,它按预期工作):
var myModule = angular.module('myModule', []);
myModule.factory('myService', function(){
var serviceImplementation = {};
serviceImplementation.one = 1;
serviceImplementation.two = 2;
serviceImplementation.three = 3;
return serviceImplementation
});
Run Code Online (Sandbox Code Playgroud)
当我试图孤立地测试服务时,我应该能够访问它并检查它们的方法.我的问题是:如何在不引导AngularJS的情况下在我的测试中注入服务?
例如,我如何测试使用Jasmine的服务方法返回的值,如下所示:
describe('myService test', function(){
describe('when I call myService.one', function(){
it('returns 1', function(){
myModule = angular.module('myModule');
//something is missing here..
expect( myService.one ).toEqual(1);
})
})
});
Run Code Online (Sandbox Code Playgroud) 我正在使用Jasmine Enzyme浅层渲染测试React组件.
为了这个问题,这里简化了......
function MyOuterComponent() {
return (
<div>
...
<MyInnerComponent title="Hello" />
...
<MyInnerComponent title="Good-bye" />
...
</div>
)
}
Run Code Online (Sandbox Code Playgroud)
MyOuterComponent有2个实例,MyInnerComponent我想测试每个的道具.
第一个我知道如何测试.我用find用first...
expect(component.find('MyInnerComponent').first()).toHaveProp('title', 'Hello');
Run Code Online (Sandbox Code Playgroud)
但是,我正在努力测试第二个实例MyInnerComponent.
我希望这样的东西能起作用......
expect(component.find('MyInnerComponent').second()).toHaveProp('title', 'Good-bye');
Run Code Online (Sandbox Code Playgroud)
甚至这......
expect(component.find('MyInnerComponent')[1]).toHaveProp('title', 'Good-bye');
Run Code Online (Sandbox Code Playgroud)
但当然上述工作都没有.
我觉得我错过了显而易见的事.
但是,当我查看文档时,我没有看到类似的例子.
任何人?