ReferenceError:mock未定义Javascript

时间:2016-07-13 19:49:46

标签: javascript jsmockito

启动所有内容的主要js文件:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>InterfaceTest</title>
  <link rel="stylesheet" href="https://code.jquery.com/qunit/qunit-2.0.0.css">
  <script type="text/javascript" src="https://rawgit.com/angular/bower-angular-mocks/master/angular-mocks.js"></script>

  <script src="https://code.jquery.com/qunit/qunit-2.0.0.js"></script>
  <script type="text/javascript" src="http://code.jquery.com/jquery-2.2.4.min.js"></script>
  <script type="text/javascript" src="https://rawgit.com/danielfm/jshamcrest/master/src/jshamcrest.js"></script>
  <script type="text/javascript" src="https://rawgit.com/cleishm/jsmockito/master/src/jsmockito.js"></script>

  <script src="Interface.js" ></script>
  <script src="InterfaceTest.js" ></script>
</head>
<body>
  <div id="qunit"></div>
  <div id="qunit-fixture"></div>
</body>
</html>

InterfaceTest:

QUnit.test("mockitoTest", function(assert) {
    var mockedObject = mock(Array);
    when(mockedObject).get(1).thenReturn("hello world");
    alert(mockedObject.get(1));
});

我需要使用Jquery,Hamcrest和JsMockito为Interface进行一系列单元测试(无关紧要)。

我得到的错误:

Died on test #1     at file:///C:/Internship/JavaScript/Qunit/InterfaceTest.js:1:7: mock is not defined@ 
Source:     
ReferenceError: mock is not defined
    at Object.<anonymous> (file:///C:/Internship/JavaScript/Qunit/InterfaceTest.js:2:21)

我确定这是显而易见的,但我已将Mockito作为依赖项包含在内,所以我不知道为什么它说mock没有定义。我的假设是

 <script `type="text/javascript" src="https://rawgit.com/cleishm/jsmockito/master/src/jsmockito.js"></script>

一定是错的。

(我在另一个stackoverflow帖子上看到,如果你在https://rawgit.com/上发布github链接,那么你可以把它包含在src中,这就是这些链接的来源)

1 个答案:

答案 0 :(得分:1)

当我运行您的代码时,我看到错误Can not set mock of undefined并指向angular-mocks.js文件。

文件angular-mocks.js期待角度,但您不加载角度。