【问题标题】:JavaScript ReferenceError - object is not defined when running npm testJavaScript ReferenceError - 运行 npm test 时未定义对象
【发布时间】:2019-07-09 15:16:43
【问题描述】:

很抱歉,如果这个问题的答案真的很明显,但我无法找出问题所在。

我制作了一个简单的账单拆分应用程序,一切正常。我正在尝试编写测试并且正在使用 Jasmine。

我的代码如下:

    function Bill_Splitter(){

      this.amount = 0;

    };

我的规格是:

   describe('Bill_Splitter', function() {
     var splitter;

     beforeEach(function() {
       splitter = new Bill_Splitter();
     });

     describe('splitter', function() {
       it('has a default amount of 0', function(){
         expect(splitter.amount).toEqual(0);
       });
     });
   });

当我在终端运行npm test时,错误如下:

ReferenceError: Bill_Splitter is not defined

谁能帮忙解释一下这是为什么?我对 JavaScript 比较陌生!

谢谢:)

更新:

文件目录:

.
├── README.md
├── girl-and-money.png
├── index.html
├── node_modules
│   ├── balanced-match
│   │   ├── LICENSE.md
│   │   ├── README.md
│   │   ├── index.js
│   │   └── package.json
│   ├── brace-expansion
│   │   ├── LICENSE
│   │   ├── README.md
│   │   ├── index.js
│   │   └── package.json
│   ├── concat-map
│   │   ├── LICENSE
│   │   ├── README.markdown
│   │   ├── example
│   │   │   └── map.js
│   │   ├── index.js
│   │   ├── package.json
│   │   └── test
│   │       └── map.js
│   ├── fs.realpath
│   │   ├── LICENSE
│   │   ├── README.md
│   │   ├── index.js
│   │   ├── old.js
│   │   └── package.json
│   ├── glob
│   │   ├── LICENSE
│   │   ├── README.md
│   │   ├── changelog.md
│   │   ├── common.js
│   │   ├── glob.js
│   │   ├── package.json
│   │   └── sync.js
│   ├── inflight
│   │   ├── LICENSE
│   │   ├── README.md
│   │   ├── inflight.js
│   │   └── package.json
│   ├── inherits
│   │   ├── LICENSE
│   │   ├── README.md
│   │   ├── inherits.js
│   │   ├── inherits_browser.js
│   │   └── package.json
│   ├── jasmine
│   │   ├── Gruntfile.js
│   │   ├── MIT.LICENSE
│   │   ├── README.md
│   │   ├── bin
│   │   │   └── jasmine.js
│   │   ├── lib
│   │   │   ├── command.js
│   │   │   ├── examples
│   │   │   │   └── jasmine.json
│   │   │   ├── filters
│   │   │   │   └── console_spec_filter.js
│   │   │   ├── jasmine.js
│   │   │   └── reporters
│   │   │       ├── completion_reporter.js
│   │   │       └── console_reporter.js
│   │   └── tasks
│   │       └── jasmine.js
│   ├── jasmine-core
│   │   ├── CODE_OF_CONDUCT.md
│   │   ├── MANIFEST.in
│   │   ├── MIT.LICENSE
│   │   ├── README.md
│   │   ├── RELEASE.md
│   │   ├── bower.json
│   │   ├── ci.js
│   │   ├── images
│   │   │   ├── __init__.pyc
│   │   │   ├── __pycache__
│   │   │   │   └── __init__.cpython-36.pyc
│   │   │   ├── jasmine-horizontal.png
│   │   │   ├── jasmine-horizontal.svg
│   │   │   └── jasmine_favicon.png
│   │   ├── jasmine_core.egg-info
│   │   │   ├── PKG-INFO
│   │   │   ├── SOURCES.txt
│   │   │   ├── dependency_links.txt
│   │   │   ├── requires.txt
│   │   │   └── top_level.txt
│   │   ├── lib
│   │   │   ├── jasmine-core
│   │   │   │   ├── boot.js
│   │   │   │   ├── example
│   │   │   │   │   ├── node_example
│   │   │   │   │   │   ├── lib
│   │   │   │   │   │   │   └── jasmine_examples
│   │   │   │   │   │   │       ├── Player.js
│   │   │   │   │   │   │       └── Song.js
│   │   │   │   │   │   └── spec
│   │   │   │   │   │       ├── helpers
│   │   │   │   │   │       │   └── jasmine_examples
│   │   │   │   │   │       │       └── SpecHelper.js
│   │   │   │   │   │       └── jasmine_examples
│   │   │   │   │   │           └── PlayerSpec.js
│   │   │   │   │   ├── spec
│   │   │   │   │   │   ├── PlayerSpec.js
│   │   │   │   │   │   └── SpecHelper.js
│   │   │   │   │   └── src
│   │   │   │   │       ├── Player.js
│   │   │   │   │       └── Song.js
│   │   │   │   ├── jasmine-html.js
│   │   │   │   ├── jasmine.css
│   │   │   │   ├── jasmine.js
│   │   │   │   ├── json2.js
│   │   │   │   └── node_boot.js
│   │   │   └── jasmine-core.js
│   │   ├── package.json
│   │   └── requirements.txt
│   ├── minimatch
│   │   ├── LICENSE
│   │   ├── README.md
│   │   ├── minimatch.js
│   │   └── package.json
│   ├── once
│   │   ├── LICENSE
│   │   ├── README.md
│   │   ├── once.js
│   │   └── package.json
│   ├── path-is-absolute
│   │   ├── index.js
│   │   ├── license
│   │   ├── package.json
│   │   └── readme.md
│   ├── spec
│   │   ├── billSplitterSpec.js
│   │   └── support
│   │       └── jasmine.json
│   ├── src
│   │   └── billSplitter.js
│   └── wrappy
│       ├── LICENSE
│       ├── README.md
│       ├── package.json
│       └── wrappy.js
├── package-lock.json
├── package.json
└── skeleton.css

【问题讨论】:

  • 这两部分代码是否在同一个文件中?
  • 否 - top 在 Src 文件中的 billSplitter.js 中,第二个在 Spec 文件中的 billSplitterSpec.js 中
  • 你需要在测试文件中要求它然后const Bill_Splitter = require('./Bill_splitter')

标签: javascript testing jasmine


【解决方案1】:

你需要require那些你想在测试中使用的文件:

// Bill_Splitter.js
module.exports = function(){
    this.amount = 0;
};
// billSplitterSpec.js
const Bill_Splitter = require('../src/billSplitter')

describe('Bill_Splitter', function() {
    var splitter;

    beforeEach(function() {
        splitter = new Bill_Splitter();
    });

    describe('splitter', function() {
        it('has a default amount of 0', function(){
            expect(splitter.amount).toEqual(0);
        });
    });
});

【讨论】:

  • 谢谢! :) 我现在收到错误消息:Cannot find module './billSplitter'(billSplitter 是文件名)
  • 这两个文件在同一个目录吗? billSplitter 和您的 specs 文件。
  • src 文件中没有 billSplitter,而 spec 文件中没有 billSplitterSpec。两个文件都在同一个目录中
  • 您可以在图片中分享您的目录结构还是...?在此之前试试这个:const Bill_Splitter = require('../src/billSplitter')
  • 这很奇怪! const Bill_Splitter = require('../src/billSplitter') 应该可以解决问题,但我真的不知道为什么没有。请注意,您的所有文件都驻留在node_modules 上,不应该这样!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-21
  • 1970-01-01
相关资源
最近更新 更多