Sta*_*ers 1 jasmine angularjs angular-mock
我想使用茉莉花的上下文,所以我可以组织我的模拟返回.这是一些伪代码来演示我想要做的事情.我希望这两个期望都能通过:
describe('a module', function(){
var whatTheFunctionReturns;
beforeEach(function(){
module('anApp', function($provide){
$provide.value('aFactory', { aFunction: whatTheFunctionReturns })
}
});
describe('when the function returns alpha', function(){
whatTheFunctionReturns = 'alpha'
it('should get data from a service', function(){
expect(aFactory.aFunction).toEqual( 'alpha' )
});
});
describe('when the function returns beta', function(){
whatTheFunctionReturns = 'beta'
it('should get data from a service', function(){
expect(aFactory.aFunction).toEqual( 'beta' )
});
});
});
Run Code Online (Sandbox Code Playgroud)
请仔细阅读以上内容.你看到我想做什么了吗?代码
$provide.value('aFactory', { aFunction: whatTheFunctionReturns })
Run Code Online (Sandbox Code Playgroud)
在beforeEach块中写入一次,但变量
whatTheFunctionReturns
Run Code Online (Sandbox Code Playgroud)
在两个描述的块中更改,when the function returns alpha并且when the function returns beta.
但是,它不起作用.这是一些真正的代码,我正在尝试测试控制器并模拟它依赖的工厂:
describe('firstController', function(){
var $rootScope, $scope, $controller
var message = 'I am message default'
beforeEach(function(){
module('App',function($provide){
$provide.value('ServiceData', { message: message})
});
inject(function(_$rootScope_,_$controller_){
$rootScope = _$rootScope_
$scope = $rootScope.$new()
$controller = _$controller_
$controller('firstController', { '$rootScope' : $rootScope, '$scope' : $scope })
});
});
describe('when message 1', function(){
beforeEach(function(){
message = 'I am message one'
});
it('should get data from a service', function(){
expect($scope.serviceData.message).toEqual( '1' ) // using wrong data so I can see what data is being returned in the error message
});
});
describe('when message 2', function(){
beforeEach(function(){
message = 'I am message two'
});
it('should get data from a service', function(){
expect($scope.serviceData.message).toEqual( '2' ) // using wrong data so I can see what data is being returned in the error message
});
});
});
Run Code Online (Sandbox Code Playgroud)
这是我收到的错误消息:
Firefox 34.0.0 (Ubuntu) firstController when message 1 should get data from a service FAILED
Expected 'I am message default' to equal '1'.
Firefox 34.0.0 (Ubuntu) firstController when message 2 should get data from a service FAILED
Expected 'I am message one' to equal '2'.
Run Code Online (Sandbox Code Playgroud)
这是一半的工作.变量正在更新,但仅在最后一个描述块('when message 2')中.这是我期望返回的内容:
Firefox 34.0.0 (Ubuntu) firstController when message 1 should get data from a service FAILED
Expected 'I am message one' to equal '1'.
Firefox 34.0.0 (Ubuntu) firstController when message 2 should get data from a service FAILED
Expected 'I am message two' to equal '2'.
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?你看到我正在尝试用describe块做什么吗?
你误解了Jasmine在执行每个测试之前是如何构建测试用例的.
看看这个并执行它:
describe("Test", function(){
console.info("Calling beforeEach() before describe()");
beforeEach(function(){
console.info("Running before()");
});
console.info("Calling describe() A");
describe("describe A", function(){
console.info("Calling it() A 0");
it('should A 0', function(){
console.info("Running it() A 1");
expect("test to be").toBe("implemented");
});
console.info("Calling it() A 1");
it('should A 1', function(){
console.info("Running it() A 2");
expect("test to be").toBe("implemented");
});
console.info("Calling it() A 2");
it('should A 2', function(){
console.info("Running it() A 3");
expect("test to be").toBe("implemented");
});
});
});
Run Code Online (Sandbox Code Playgroud)
在控制台中,您将观察到:
Calling beforeEach() before describe()
Calling describe() A
Calling it() A 0
Calling it() A 1
Calling it() A 2
Running before()
Running it() A 1
Running before()
Running it() A 2
Running before()
Running it() A 3
Run Code Online (Sandbox Code Playgroud)
这里发生了什么?
当您调用describe()回调函数中的代码时,将执行您提供的代码.对其他describe()s的后续调用将执行其回调.每次调用it(),beforeEach()并将afterEach()在内部队列树中排队传递的回调,before将被添加到每个分支,after将被附加到该分支.然后队列被逐个移动,Jasmine将为每一步执行存储的回调.
当寻找到你的第一个代码,这意味着你的所有任务whatTheFunctionReturns被执行,则每个it()(由preceeded beforeEach())正在执行
你应该做什么:
describe('a module', function(){
var whatTheFunctionReturns;
// pepare to run in beforeEach()
function _beforeModule(){
module('anApp', function($provide){
$provide.value('aFactory', { aFunction: whatTheFunctionReturns })
}
}
describe('when the function returns alpha', function(){
beforeEach(function(){
whatTheFunctionReturns = 'alpha';
_beforeModule();
});
it('should get data from a service', function(){
expect(aFactory.aFunction).toEqual( 'alpha' )
});
});
describe('when the function returns beta', function(){
beforeEach(function(){
whatTheFunctionReturns = 'beta';
_beforeModule();
});
it('should get data from a service', function(){
expect(aFactory.aFunction).toEqual( 'beta' )
});
});
});
Run Code Online (Sandbox Code Playgroud)
您必须将赋值包装beforeEach()或it()块.
| 归档时间: |
|
| 查看次数: |
2035 次 |
| 最近记录: |