Reputation: 23132
I am using jasmine.version "2.2.0". I am getting "Spies must be created in a before function or a spec" on my very basic test. What can be wrong? Please see code below;
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<img src="../Content/jasmine/jasmine_favicon.png" />
<link href="../Content/jasmine/jasmine.css" rel="stylesheet" />
<script src="../Scripts/jasmine/jasmine.js"></script>
<script src="../Scripts/jasmine/jasmine-html.js"></script>
<script src="../Scripts/jasmine/boot.js"></script>
</head>
<body>
<script id="myRealCodeBase" type="text/javascript">
var objectUnderTest = {
someFunction: function (arg1, arg2) {
var result = arg1 + arg2;
return result;
}
};
</script>
<script id="testScripts" type="text/javascript">
spyOn(objectUnderTest, 'someFunction');
//Call the method with specific arguments
objectUnderTest.someFunction('param1', 'param2');
//Get the arguments for the first call of the function
var callArgs = objectUnderTest.someFunction.call.argsFor(0);
console.log(callArgs);
//displays ['param1','param2']
</script>
</body>
</html>
Upvotes: 4
Views: 6971
Reputation: 366
Well, you are not creating a test (spec) or using a before function, so that must be the problem.
You should wrap your tests in suites and specs, as they are called in jasmine, like this:
describe('A simple test', function () {
beforeEach() {
// callthrough is used to call the actual function, and not just mocking the call
spyOn(objectUnderTest, 'someFunction').and.callThrough();
};
it('should add two numbers', function () {
var sum = objectUnderTest.someFunction(1, 2);
expect(sum).toEqual(3);
expect(objectUnderTest.someFunction).toHaveBeenCalled();
});
});
That is a simple test using jasmine. It is very simple, but it needs some structure to work. It is a very powerful framework, and once you get the hang of it, it is pretty simple and fun to write tests =)
Upvotes: 6
Reputation: 549
You're tests should be wrapped in a describe and an it block.
describe("A suite", function() {
it("contains spec with an expectation", function() {
expect(true).toBe(true);
});
});
Upvotes: 1