在Rspec测试中,我可以避免两次调用控制器吗?

时间:2018-01-19 09:17:23

标签: ruby-on-rails ruby rspec

我想在我的控制器测试中避免双重调用,以获得更好的性能。 我正在使用Rails。

我的测试是这样的:

before { get :index }
it { expect(response).not_to be_successful }
it { expect(Bugsnag).to receive(:notify)
after { get :index }

我想要的是以下内容,我只需拨打一次GET #index

it { expect(Bugsnag).to receive(:notify)
before { get :index }
it { expect(response).not_to be_successful }

我遇到的问题是expect(class).to receive(:message)之前需要调用get :index

我该如何设置?这可能吗?

2 个答案:

答案 0 :(得分:2)

您可以使用partial double

来完成此操作
const async = require("async");
const request = require("request");

let page = 1;
let json;

async.whilst(function () {
    // condition here
  json.data.length !== 0
},
function (next) {
  request(`https://driftrock-dev-test-2.herokuapp.com/purchases?${page}&per_page=20`, function (error, response, body) {
    if (!error && response.statusCode == 200) {
    json = JSON.parse(body);
        console.log(json.data);      
        console.log(page);
    }
    page++;
    next();
  });
},
function (err) {
  // All things are done!
});

答案 1 :(得分:0)

更多代码行,但是:

  • 没有get :index
  • 的双重调用
  • 现在每个例子都有描述

-

it 'is not successful' do
  get :index
  expect(response).not_to be_successful
end

it 'notifies someone' do
  expect(Bugsnag).to receive(:notify)
  get :index
end
相关问题