JavaScript ReferenceError-运行npm测试时未定义对象

时间:2019-07-09 15:16:43

标签: javascript testing jasmine

很抱歉,如果答案确实很明显,但是我无法弄清楚哪里出了问题。

我制作了一个简单的账单拆分应用程序,一切正常。我正在尝试编写测试,并且正在使用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

1 个答案:

答案 0 :(得分:0)

您需要以这种方式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);
        });
    });
});