简体   繁体   English

使用夹具在轨道上的红宝石

[英]ruby on rails using fixtures

I am using fixtures in testing. 我在测试中使用夹具。

For example: 例如:

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"
      } }

Is it possible to replace 是否可以更换

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

with one fixture call? 一个夹具电话?

Of course, assuming all that params are from one object and use rspec with it, you can do this: 当然,假设所有参数均来自一个对象,并对其使用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

If for example they are from one same model but from 2 different objects (like account_1 and account_2), then just substitute with this: 例如,如果它们来自同一模型但来自2个不同的对象(例如account_1和account_2),则只需用以下内容替换:

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

I used a wrong approach to solve the issue. 我使用了错误的方法来解决此问题。

Now, I simply defined a method in the test_helper.rb with: 现在,我只需在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"
      } }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM