admin管理员组文章数量:1312778
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="">
<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>
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/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>
Share
Improve this question
asked Sep 15, 2015 at 18:05
Teoman shipahiTeoman shipahi
23.1k16 gold badges144 silver badges173 bronze badges
1
-
1
Take a look at the docs and try to move your test code inside a spec (
it
block) – Michael Radionov Commented Sep 15, 2015 at 18:11
2 Answers
Reset to default 6Well, 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 =)
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);
});
});
本文标签: javascriptSpies must be created in a before function or a specStack Overflow
版权声明:本文标题:javascript - Spies must be created in a before function or a spec - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741897109a2403641.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论