Jasmine jQuery loadFixtures() 导致“TypeError: n.ajax is not a function”错误

时间:2021-01-06 01:58:10

标签: html url jasmine fixtures jasmine-jquery

这是我的第一篇堆栈溢出帖子,如果我的问题不清楚或缺乏细节,请见谅!

我一整天都在努力解决这个问题!我正在使用 Jasmine JQuery 库来尝试测试我的 JavaScript 是否正确操作 DOM。我想知道我使用的welcomeModalFixture.html 固定装置的路径是否不正确(我正在尝试使用jasmine.getFixtures().fixturesPath)或者welcomeModalFixture.html 文件的内容是否有问题(是head和用作夹具的 html 文件所需的 html 标签?)或者我需要做其他事情才能使其在 spec.js 文件中工作????

在浏览器 (Chrome) 中打开 specRunner.html 时出现以下错误:

TypeError: n.ajax is not a function
    at t.Fixtures.loadFixtureIntoCache_ (https://cdn.jsdelivr.net/npm/jasmine-jquery@2.1.1/lib/jasmine-jquery.min.js:7:1887)
    at t.Fixtures.getFixtureHtml_ (https://cdn.jsdelivr.net/npm/jasmine-jquery@2.1.1/lib/jasmine-jquery.min.js:7:1739)
    at t.Fixtures.read (https://cdn.jsdelivr.net/npm/jasmine-jquery@2.1.1/lib/jasmine-jquery.min.js:7:1090)
    at t.Fixtures.load (https://cdn.jsdelivr.net/npm/jasmine-jquery@2.1.1/lib/jasmine-jquery.min.js:7:871)
    at t.Fixtures.proxyCallTo_ (https://cdn.jsdelivr.net/npm/jasmine-jquery@2.1.1/lib/jasmine-jquery.min.js:7:2629)
    at e.loadFixtures (https://cdn.jsdelivr.net/npm/jasmine-jquery@2.1.1/lib/jasmine-jquery.min.js:7:12580)
    at Suite.<anonymous> (https://5500-eb70146a-2e69-4a57-9689-b57b62501c65.ws-us03.gitpod.io/assets/jasmine/spec.js:15:5)
    at <Jasmine>

这是我的文件结构:

Project
 |
 +-- assets
 |  |
 |  +-- jasmine
    |  |
    |  +-- spec.js
    |  +-- specRunner.html
    |  +-- fixtures
    |     |
    |     +-- welcomeModalFixture.html
    +-- js
       |
       +-- main.js
       +-- modules
          |
          +-- modals.js

这是我在 spec.js 中的代码:

// test
console.log("spec.js module has run");

// imports
import * as modals from "../js/modules/modals.js";

jasmine.getFixtures().fixturesPath = './fixtures';


/* Tests for welcome modal form */

describe("Welcome modal form", function(){
    
    // load before each spec so that all specs are independent
    loadFixtures("welcomeModalFixture.html");
    

    it("startGame function should be defined", function() {
        expect(modals.startGame).toBeDefined();
    });
});

这是我在welcomeModalFixture.html 文件中的代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
<div class="modal show" id="welcomeModal" data-backdrop="static" data-keyboard="false" tabindex="-1" aria-label="Welcome-to-the-game" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
        <! -- Modal content here -->
    </div>
  </div>
</div>     
</body>
</html>

这是我的 specRunner.html 文件:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Jasmine specRunner</title>

    <!-- Load jQuery (slim build - excludes "effects, ajax, and deprecated code") -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.slim.min.js" integrity="sha512-/DXTXr6nQodMUiq+IUJYCt2PPOUjrHJ9wFrqpJ3XkgPNOZVfMok7cRw6CSxyCQxXn6ozlESsSh1/sMCTF1rL/g==" crossorigin="anonymous"></script>

    <!-- Load Jasmine Framework from CDNs -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/3.6.0/jasmine.js" integrity="sha512-dqL4nYnbiMiNNy/V2zRK55QXSRVyu26KCuGoL8Zf5lDfKRRf8pBFW8RXe8at36QlB8s8bnOjKP6b5YTalHO6wQ==" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/3.6.0/jasmine-html.js" integrity="sha512-bqig44hBpNuvBzxsqbHiqwRximGm4UfJAvOeNQHjB7++0D2u/3bcw4ZwmVsqrooii2UJ91oDI/6aANdp3BwcQQ==" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/3.6.0/boot.js" integrity="sha512-igPP1Xd0hsvXVZGyfIYeUD779NLEA1Sj/5ts2C/e/22zeUqR6ehA7M/nNLuONalznkHQkGPBESNwzGpwBQD60g==" crossorigin="anonymous"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jasmine/3.6.0/jasmine.css" integrity="sha512-XKXP02aeSNI5HsDB3+4MM0tSDXJTBFZpt99xL7PvMz3+A3aRy0F0Ne/CATK+Z72ckE/4Ev9OiosWQr5LtU284Q==" crossorigin="anonymous" />

    <script src="https://cdn.jsdelivr.net/npm/jasmine-jquery@2.1.1/lib/jasmine-jquery.min.js"></script>

    <!-- Load js source -->
    <script src="../js/main.js" type="module"></script>

    <!-- Load Specs (loaded after source so that objects in source can be referenced by specs) -->
    <script src="./spec.js" type="module"></script>

</head>
<body>
    
</body>
</html>

请帮忙!!!! :):):)

0 个答案:

没有答案