Mocha - 测试Promise,`done`永远不会在Promise中被调用

时间:2015-04-22 23:42:43

标签: javascript unit-testing promise mocha

我试图测试一个会在.then块的done块中调用的间谍,但then块中的timeout of 2000ms exceeded.似乎没有完全被执行。

我得到了/** * Passed down to the LoginForm component to * handle form submission. */ _submitHandler(data) { return function(evt) { evt.preventDefault && evt.preventDefault(); evt.stopPropagation && evt.stopPropagation(); return request('post', 'auth', data) .then((res) => { AuthActions.login(); return res; }) } }

这是我正在测试的内容(异步):

describe('when it succeeds', () => {
  it('should login', (done) => {
    sinon.spy(AuthActions, 'login');

    Instance._submitHandler({})({})
      .then((res) => {
        console.log('Called!!!');
        expect(AuthActions.login.called).to.equal(true);
        AuthActions.login.restore();
        done();
      }, done);
  });
});

这是我的测试:

<template name="valueEditor">
  <div class="list-item {{editingClass}}">
    <input type="text" value="{{value}}" placeholder="value">
  </div>
</template>

我使用Karma进行测试;柴和诗乃。

2 个答案:

答案 0 :(得分:2)

我也有这个问题,原因是在打开连接之前试图回应XHR,例如。

此代码将从INVALID_STATE_ERR - 0

中抛出FakeXMLHttpRequest.setResponseHeaders
describe("get", function () {

  beforeEach(function () {
    this.xhr = sinon.useFakeXMLHttpRequest();
    this.xhr.onCreate = function (request) {
      request.respond(200, null, "");
    };
  });

  afterEach(function () {
    this.xhr.restore();
  });

  it("should make a request", function () {
    myObject.get("testurl");
  });

});

此代码有效:

describe("get", function () {

  beforeEach(function () {
    this.xhr = sinon.useFakeXMLHttpRequest();
    this.requests = [];
    this.xhr.onCreate = function (request) {
      this.requests.push(request);
    };
  });

  afterEach(function () {
    this.xhr.restore();
  });

  it("should make a request", function () {
    myObject.get("testurl");
    this.requests[0].respond(200, null, "");
  });

});

阅读文档,它本身确实显示了推送到请求数组的相同技术,我下意识地并且不准确地得到了一个印象,即onCreate,尽管它的名字,更像是&#34;请求&#34;

myObject.get = function (url) {
  var http = new XMLHttpRequest(); 
  // the XHR instance exists
  // readyState is 0
  // onCreate runs now
  http.open("GET", url); 
  http.send(); 
  // readyState is 1
  // can call setResponseHeaders and respond 
}

结果是,在调用运行XMLHttpRequest&#39; s send的方法后,您必须放置响应代码,如下:

myObject.get("testurl");
this.requests[0].respond(200, null, "");

我使用的是respond方法,但setResponseHeadersrespond调用setResponseHeaders)也是如此 - 在您的测试中,它的调用过早。< / p>

答案 1 :(得分:1)

我终于在数小时后解决了这个问题。看起来$train_list = array(); $train = $train_xpath->query('//li/div[contains(@class, "smallFont farelist no-discount")]'); if($train->length > 0) { foreach($train as $t) { $time_s = $train_xpath->evaluate('string(./div[@class="train-time"]/text()[1])', $t); $time_e = $train_xpath->evaluate('string(./div[@class="train-time"]/text()[2])', $t); $train_list[] = array( 'train_no' => $train_xpath->evaluate('string(./div[@class="train-no"])', $t), 'train_time' => "$time_s - $time_e", 'train_price' => $train_xpath->evaluate('string(./div[@class="train-info"]/div/div[@class="total-price"])', $t), ); } } 块没有被调用,因为order by `categories`.`category_name` = 'Shared article' desc 引发了异常。

让我详细说明一下。我正在使用 sinon then,如下所示:

xhr

通过在FakeXMLHttpRequest块上放置var requests, xhr; beforeEach(() => { requests = []; xhr = sinon.useFakeXMLHttpRequest(); xhr.onCreate = (req) => { req.setResponseHeaders({ /* */ }); requests.push(req); } }); ,我发现我收到的错误是console.log。这使我得出结论catch一直是问题。

然后我发现了 sinon INVALID_STATE_ERR EXCEPTION 0,并使用了它(但我不认为这实际上解决了这个问题) 。并没有真正相关,但我也在这里使用了xhr,因为我保留了无数fakeServer来写存根等等。

sandbox