Я хочу использовать контексты с жасмином, чтобы я мог организовать то, что возвратит мой мак. Вот какой-то псевдо-код, демонстрирующий, что я хочу делать. Я ожидаю, что оба эти ожидания пройдут:Использование контекстов с жасмином
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')
});
});
});
Прочтите внимательно. Вы видите, что я пытаюсь сделать? Код
$provide.value('aFactory', { aFunction: whatTheFunctionReturns })
пишется один раз в beforeEach блоке, но переменная
whatTheFunctionReturns
изменяется в двух блоков, описывают 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
});
});
});
Вот сообщение об ошибке я получаю обратно:
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'.
Это половина работает. Переменная обновляется, но только в последнем блоке описания ('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'.
Как я могу добиться этого? Вы видите, что я пытаюсь сделать с блоками описания?
Почему бы вам не сделать 'beforeEach (функция (ServiceData) { ServiceData.message = 'Я сообщение два' });' – Chandermani
@Chandermani Вы должны были бы 'впрыснуть()' а – james
вы должны принять посмотрите на sinon для насмешки/stubbing – james