最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Mocha tests mocking function - Stack Overflow

programmeradmin0浏览0评论

I'm testing backbone view, that have function:

attachSelect: function(id, route) {
    console.log(id);
    console.log(route);

    this.$(id).select2({
        ajax: {
            url: route,
            dataType: 'json',
            results: function(data) {
                var results = _.map(data, function(item) {
                    return {
                        id: item.id,
                        text: item.title
                    };
                });

                return {
                    results: results
                };
            },
            cache: true
        }
    });
}

I need to rewrite (mock) this fuction that, the looks like:

attachSelect: function(id, route) {
    console.log(id);
    console.log(route);
}

How to do that ?

I'm testing backbone view, that have function:

attachSelect: function(id, route) {
    console.log(id);
    console.log(route);

    this.$(id).select2({
        ajax: {
            url: route,
            dataType: 'json',
            results: function(data) {
                var results = _.map(data, function(item) {
                    return {
                        id: item.id,
                        text: item.title
                    };
                });

                return {
                    results: results
                };
            },
            cache: true
        }
    });
}

I need to rewrite (mock) this fuction that, the looks like:

attachSelect: function(id, route) {
    console.log(id);
    console.log(route);
}

How to do that ?

Share Improve this question edited Jul 30, 2015 at 13:55 uladzimir 5,6897 gold badges33 silver badges51 bronze badges asked Jul 30, 2015 at 13:25 WizardWizard 11.3k38 gold badges98 silver badges167 bronze badges 1
  • It depends on your test setup. You can just override view method after creating it. Like this.someView.attachSelect = function (){}; – Andrey Commented Jul 30, 2015 at 13:40
Add a comment  | 

1 Answer 1

Reset to default 14

The simplest way to mock a function is to replace the property at runtime.

You can provide your own monitoring function (commonly called a spy), although this is not the most elegant. That would look like:

var called = false;
var testee = new ViewUnderTest();
var originalAttach = testee.attachSelect; // cache a reference to the original
testee.attachSelect = function () {
  called = true;
  var args = [].concat(arguments); // get an array of arguments
  return originalAttach.apply(testee, args);
};

// Perform your test

expect(called).to.be.true;

If you have a test assertion library like chai, you can use the spies plugin and reduce that to:

var testee = new ViewUnderTest();
var spy = chai.spy(testee.attachSelect);
testee.attachSelect = spy;

// Perform your test

expect(spy).to.have.been.called();

Using a spy library will provide some useful features, such as monitoring the number of calls and their arguments to verify low-level behavior. If you're using Chai or Jasmine, I would highly suggest taking advantage of the corresponding support for spies.

发布评论

评论列表(0)

  1. 暂无评论