Skip to content

Commit

Permalink
Added a spec for testing that spies are working correctly. Thanks Dev…
Browse files Browse the repository at this point in the history
…esh Chanchlani.
  • Loading branch information
ibolmo committed Apr 18, 2011
1 parent 54071da commit 27c1b5c
Showing 1 changed file with 71 additions and 45 deletions.
116 changes: 71 additions & 45 deletions src-test/tests.js
Expand Up @@ -12,79 +12,79 @@ afterEach(function(){

describe('describe', function(){
beforeEach(function(){
depth++;
depth++;
});

afterEach(function(){
depth--;
depth--;
});

it('should map it', function(){
expect(depth).toEqual(2);
expect(depth).toEqual(2);
});

describe('nested', function(){
beforeEach(function(){
depth++;
});
beforeEach(function(){
depth++;
});

afterEach(function(){
depth--;
});
afterEach(function(){
depth--;
});

it('should exectue nested', function(){
expect(depth).toEqual(3);
});
it('should exectue nested', function(){
expect(depth).toEqual(3);
});
});
});

describe('matchers', function(){

beforeEach(function(){
this.addMatchers({
toBePersonNamed: function(name){
return this.actual == name;
}
});
this.addMatchers({
toBePersonNamed: function(name){
return this.actual == name;
}
});
});

it('should work across multiple tests', function(){
expect('misko').toBePersonNamed('misko');
expect('misko').toBePersonNamed('misko');
});

it('should allow a creation of new matcher', function(){
this.addMatchers({
toBeMe: function(){
return this.actual == 'misko';
}
});
this.addMatchers({
toBeMe2: function(arg){
return this.actual == arg;
}
});
expect('misko').toBeMe();
expect('misko').toBeMe2('misko');
expect('adam').toBePersonNamed('adam');
this.addMatchers({
toBeMe: function(){
return this.actual == 'misko';
}
});
this.addMatchers({
toBeMe2: function(arg){
return this.actual == arg;
}
});
expect('misko').toBeMe();
expect('misko').toBeMe2('misko');
expect('adam').toBePersonNamed('adam');
});
});

describe('runs', function(){
it('should execute a runs block', function(){
runs(function(){
this.runsFunction = function(){
return true;
};
spyOn(this, 'runsFunction');
});

runs(function(){
this.runsFunction();
});

runs(function(){
expect(this.runsFunction).wasCalled();
});
runs(function(){
this.runsFunction = function(){
return true;
};
spyOn(this, 'runsFunction');
});

runs(function(){
this.runsFunction();
});

runs(function(){
expect(this.runsFunction).wasCalled();
});
});
});

Expand Down Expand Up @@ -113,4 +113,30 @@ describe('a failing suite, ignore', function(){
});
});

describe('should support multiple spies and subsequent actual calls', function(){
var testObject = {
functionToBeSpied : function(){
return -1;
},

caller : function(){
return this.functionToBeSpied();
}
};

it('first test case to spy the function', function(){
spyOn(testObject, 'functionToBeSpied').andReturn(7);
expect(testObject.caller()).toEqual(7);
});

it('second test case to spy the function', function(){
spyOn(testObject, 'functionToBeSpied').andReturn(70);
expect(testObject.caller()).toEqual(70);
});

it('last test case which calls the actual function', function(){
expect(testObject.caller()).toEqual(-1);
});
});

})();

0 comments on commit 27c1b5c

Please sign in to comment.