伪装者和查询参数

时间:2014-11-14 20:19:50

标签: ember.js integration-testing qunit

我使用Trek的伪装程序库成功,只要我没有任何查询参数。

我现在花了4个小时盯着下面的代码并且无法让它发挥作用。我定义了一个似乎没有被击中的网址。但是,当我看到尚未被捕获的呼叫时,它会返回我定义的确切网址!如果有人可以帮我解决这个问题,那就太好了。

我的测试代码:

var server;

module("Check Search Index page", {
  setup: function() {
    'use strict';

    Ember.run(function() {
      App.reset();

      server = new Pretender(function() {
        this.get('/api/v1/holidays.json?display_type=detail&page=1&sort%5Bprice%5D=&sort%5Bname%5D=&sort%5Brating%5D=&filter%5Bduration_min%5D=1&filter%5Bduration_max%5D=32&filter%5Bprice_min%5D=50&filter%5Bprice_max%5D=3800&filter%5Bbus%5D=true&filter%5Bflight%5D=true&filter%5Bself_transportation%5D=true', function (request) {
          return [200, {'Content-Type': 'application/json'}, '{"holidays":[{"id":507,"name":"App. Elena"}'];
        });
      });

      server.unhandledRequest = function(verb, path, request) {
        console.log("=== BEGIN UNHANDLED REQUEST ===");
        console.log('verb: ' + verb);
        console.log('path: ' + path);
        console.log('request: ' + request);
        console.log("=== END UNHANDLED REQUEST ===");
      };
    });
  },

  teardown: function() {
    'use strict';

    server.shutdown();
  }
});

test('Search page', function() {
  'use strict';

  visit('/search/index');
  andThen(function() {
    ok(find('p:contains("Bepaal uw zoek criteria")').length, 'Search page is showing');
  });
});

我在控制台中收到的错误消息:

.=== BEGIN UNHANDLED REQUEST ===
verb: GET
path: /api/v1/holidays.json?display_type=detail&page=1&sort%5Bprice%5D=&sort%5Bname%5D=&sort%5Brating%5D=&filter%5Bduration_min%5D=1&filter%5Bduration_max%5D=32&filter%5Bprice_min%5D=50&filter%5Bprice_max%5D=3800&filter%5Bbus%5D=true&filter%5Bflight%5D=true&filter%5Bself_transportation%5D=true
request: [object Object]
=== END UNHANDLED REQUEST ===

1 个答案:

答案 0 :(得分:2)

您不应在请求定义中定义查询参数。要使上面的代码工作,终点的定义应该是:

  server = new Pretender(function() {
    this.get('/api/v1/holidays.json?', function (request) {
      return [200, {'Content-Type': 'application/json'}, '{"holidays":[{"id":507,"name":"App. Elena"}'];
    });
  });