必须在之前的函数或规范

时间:2015-09-15 18:05:57

标签: javascript jasmine

我正在使用jasmine.version“2.2.0”。在我的基本测试中,我得到“间谍必须在之前的功能或规范中创建”。 有什么不对?请参阅下面的代码;

<!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>

2 个答案:

答案 0 :(得分:3)

嗯,你没有创建测试(spec)或使用before函数,所以这一定是问题所在。

你应该在套件和规范中包装你的测试,因为它们在jasmine中调用,如下所示:

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();
    });
});

这是一个使用茉莉花的简单测试。这很简单,但需要一些结构才能工作。它是一个非常强大的框架,一旦掌握了它,编写测试就非常简单有趣了=)

答案 1 :(得分:1)

你的测试应该包含在描述和阻止中。

describe("A suite", function() {
  it("contains spec with an expectation", function() {
    expect(true).toBe(true);
  });
});
相关问题