将上下文传递到mocha中的共享测试中获取未定义的错误

时间:2017-08-11 17:17:04

标签: javascript mocha

我目前正在关注我的mocha,sinon和chai测试的共享行为示例。

https://github.com/mochajs/mocha/wiki/Shared-Behaviours

目前,我在测试应用程序时会尝试重复使用测试。

在我的共享测试中,我传递了一个context,它将传递一个React商店的对象组件,我称之为var testObj = new ReactStore(data, ContainerModel);

问题

当我从共享测试文件导入共享测试时,我无法弄清楚如何使用我的共享测试。

这就是SharedTest.js的结构如何

   var shouldBehaveLikeACustomDomain = function (context) {
    //custom domain test
    describe("Custom Domain", function () {
        var domainData = context.domainData;
        //Methods for extended class
        describe("Methods", function () {
            //test for refresh method
            it("_refresh should call __onRefresh", function (done) {
                var customDomainClass = class extends context.domain {
                    constructor (domainData, StoreContainer) {
                        super(domainData, StoreContainer);
                    }
                    //recaluclate some properties every time domainData is refreshed
                    __onRefresh() {
                        assert(true);
                        done();
                    }
                };
                var url = new RegExp("^http\\:\\/\\/example\\.com\\/api\\/logEntries\\/" + domainData + "(\\?(.*))?$", "i");
                //server sends get request
                context.respondWith("GET", url, function (request) {
                    try {
                        request.respond(200, {}, "{}");
                    }
                    catch (e) {
                        done(e);
                    }
                });
                var testCustomObj = new customDomainClass(domainData, StoreContainer);
                testCustomObj._refresh();
            });

            //test load
            it ("#Actions:load should call __.onLoad()", function () {
                var isLoaded = false,
                    customDomainClass = class extends context.domain {
                        constructor (domainData, StoreContainer) {
                            super({}, domainData, StoreContainer);
                        }
                        //keep custom property up-to-date by using an onLoad hook
                        __onLoad() {
                            isLoaded = true;
                        }
                    };
                //create default response
                context.respondWith(SinonUtil.persistentResponse);
                //create instance
                var testCustomObj = new customDomainClass(domainData,StoreContainer);
                testCustomObj.Actions().load(testCustomObj.get('id'));
                assert.equal(isLoaded, true);
            });
        });
    });
};
module.exports = shouldBehaveLikeACustomDomain;

AnotherTestFile.js

var shared = require(./SharedTest.js);

describe ('#Domains', function () {
 beforeEach(function (){
   var testObj = new ReactStore(data, ContainerModel);
  });
   shared.shouldBehaveLikeACustomDomain();

 //some other tests here
});

这是我得到的错误

    C:\Users\Demon\Desktop\hapnin.js\test\EventStore.test.js:217
        shared.shouldBehaveLikeACustomDomain(testObj);
               ^

TypeError: shared.shouldBehaveLikeACustomDomain is not a function
    at Suite.<anonymous> (C:\Users\Demon\Desktop\hapnin.js\test\EventStore.test.js:217:16)
    at Object.create (C:\Users\Demon\Desktop\hapnin.js\node_modules\mocha\lib\interfaces\common.js:114:19)
    at context.describe.context.context (C:\Users\Demon\Desktop\hapnin.js\node_modules\mocha\lib\interfaces\bdd.js:44:27)
    at Suite.<anonymous> (C:\Users\Demon\Desktop\hapnin.js\test\EventStore.test.js:215:5)
    at Object.create (C:\Users\Demon\Desktop\hapnin.js\node_modules\mocha\lib\interfaces\common.js:114:19)
    at context.describe.context.context (C:\Users\Demon\Desktop\hapnin.js\node_modules\mocha\lib\interfaces\bdd.js:44:27)
    at Object.<anonymous> (C:\Users\Demon\Desktop\hapnin.js\test\EventStore.test.js:166:1)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at C:\Users\Demon\Desktop\hapnin.js\node_modules\mocha\lib\mocha.js:230:27
    at Array.forEach (native)
    at Mocha.loadFiles (C:\Users\Demon\Desktop\hapnin.js\node_modules\mocha\lib\mocha.js:227:14)
    at Mocha.run (C:\Users\Demon\Desktop\hapnin.js\node_modules\mocha\lib\mocha.js:495:10)
    at Object.<anonymous> (C:\Users\Demon\Desktop\hapnin.js\node_modules\mocha\bin\_mocha:469:18)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:394:7)
        at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:509:3

0 个答案:

没有答案