Closed nareshbhatia closed 9 years ago
It is actually more equivalent to this:
var $controller,
$rootScope,
$q,
dataservice;
beforeEach(inject(function(_$controller_, _$q_, _$rootScope_, _dataservice_) {
$controller = _$controller_;
$q = _$q_;
$rootScope = _$rootScope_;
dataservice = _dataservice_;
}
afterEach(function() {
$controller = null;
$q = null;
$rootScope = null;
dataservice = null;
}
Well actually it is exactly like this:
beforeEach(inject(function($controller, $q, $rootScope, dataservice) {
window.$controller = $controller;
window.$q = $q;
window.$rootScope = $rootScope;
window.dataservice = dataservice;
}));
afterEach(function() {
delete window.$controller;
delete window.$q;
delete window.$rootScope;
delete window.dataservice;
});
Then your it
test simply consumes these globals:
it('has a dataservice', function() {
expect(dataservice).to.exist();
});
The objective is to eliminate the tedious variable definition and assignment statements that crowd the top of a describe
block. All you do is write:
beforeEach(function(){bard.inject(function($controller, $q, $rootScope, dataservice) {})});
or, if you prefer, this works too:
beforeEach(function(){bard.inject('$controller', '$q', '$rootScope', 'dataservice')});
and after that one-liner you write your tests:
it('has a dataservice', function() {
expect(dataservice).to.exist();
});
Of course you'd never pollute the global namespace in this fashion in production code. But it's harmless to do so in tests and there's no risk of cross-test pollution because the bard.inject
cleans up after itself by laying down that afterEach
.
_bard.js_ has other helpers too; it's not limited to inject
.
Great explanation, @johnpapa and @wardbell. Sold on the usefulness of bardjs!
Contemplating an extension. What do you all think?
I've long been bothered by the ceremony in
beforeEach(function (){ bard.inject(...); });
What if you had this sugar:
beforeEachInject(...);
Or
beforeEach.inject(...);
I like befor each separate. I may want other stuff in there.
John Papa
On Jan 20, 2015, at 12:01 PM, Ward Bell notifications@github.com wrote:
Contemplating an extension. What do you all think?
I've long been bothered by the ceremony in
beforeEach(function (){ bard.inject(...); }); What if you had this sugar:
beforeEachInject(...); Or
beforeEach.inject(...); — Reply to this email directly or view it on GitHub.
Let me clarify.
First, I'm proposing a sugar extension; you can still write it as you do today ... as we did way up above.
Second, both syntaxes support doing stuff in the body
When you pass a function to bardjs#inject, as in bard.inject(fn)
the fn is executed in the same way that it would be if you passed it to ngMocks.inject
. I don't think you lose anything. You gain less noise.
Is that gain worth adding an extension? That's my question.
meh
John Papa | @john_papa | JohnPapa.net, LLC
On Tue, Jan 20, 2015 at 12:29 PM, Ward Bell notifications@github.com wrote:
Let me clarify.
First, I'm proposing a sugar extension; you can still write it as you do today ... as we did way up above.
Second, both syntaxes support doing stuff in the body
When you pass a function to bardjs#inject, as in bard.inject(fn) the fn is executed in the same way that it would be if you passed it to ngMocks.inject. I don't think you lose anything. You gain less noise.
Is that gain worth adding an extension? That's my question.
— Reply to this email directly or view it on GitHub https://github.com/johnpapa/gulp-patterns/issues/76#issuecomment-70695325 .
Can you please explain the purpose of bardjs? It seems to me that following two are equivalent (one using bard and another without):