使用固定装置在轨道上的红宝石

时间:2017-03-30 15:55:32

标签: ruby-on-rails ruby testing fixtures

我在测试中使用灯具。

例如:

post bookings_path, params: { journal: { from_account_number: "1",
                                         from_account: "Test",
                                         from_amount: "12",
                                         to_account_number: "2",
                                         to_account: "Test2",
                                         to_amount: "12"
      } }

是否可以替换

from_account_number: "1",
from_account: "Test",
from_amount: "12",
to_account_number: "2",
to_account: "Test2",
to_amount: "12"

一次夹具调用?

2 个答案:

答案 0 :(得分:0)

当然,假设所有params来自一个对象并使用rspec,你可以这样做:

describe '#POST to bookings_path' do
  before do 
    @booking = bookings(:one)
  end

  it 'should be successful' do
    post bookings_path, params: { journal: @booking.attributes }

    expect(response.status).to eq(201)
  end
end

例如,如果它们来自同一个模型但来自2个不同的对象(例如account_1和account_2),那么只需替换为:

    data = @account_1.attributes
    post bookings_path, params: { journal: data.merge(@account_2.attributes) }

答案 1 :(得分:0)

我用错误的方法解决了这个问题。

现在,我只是在test_helper.rb中定义了一个方法:

eigenkapital_buchen{
  post bookings_path, params: { journal: { from_account_number: "1",
                                         from_account: "Test",
                                         from_amount: "12",
                                         to_account_number: "2",
                                         to_account: "Test2",
                                         to_amount: "12"
      } }
}