【问题标题】:ruby on rails using fixtures使用固定装置在轨道上安装红宝石
【发布时间】:2017-03-30 15:55:31
【问题描述】:

我在测试中使用了固定装置。

例如:

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"

只有一个夹具调用?

【问题讨论】:

  • 你应该使用工厂。 FactoryGirl 是个不错的选择

标签: ruby-on-rails ruby testing fixtures


【解决方案1】:

当然,假设所有参数都来自一个对象并使用 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) }

【讨论】:

  • 如何使用 rspec?
  • 通过将 gem 'rspec-rails' 添加到您的 gemfile 中。你用的是什么测试框架?是最小的吗?如果是这样,我会更新我的答案
  • 我正在使用 minitest。
【解决方案2】:

我使用了错误的方法来解决问题。

现在,我只是在 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"
      } }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-16
    • 2016-03-23
    • 1970-01-01
    相关资源
    最近更新 更多