测试时来自factory_girls的未初始化常量

发布于 2025-01-05 07:39:30 字数 1549 浏览 0 评论 0原文

我将 Rails 3.2.1 与factory_girl_rails一起使用,但遇到了问题,这是我的factory.rb文件:

require factory_girl
  ##defining room requests
  Factory.define :room_request do |rr|
    rr.starts_at '2012-02-06 08:00:00 UTC'
    rr.ends_at '2012-02-06 17:00:00 UTC'
    rr.request_type 'normal request'
    rr.number_of_participants 5
    rr.bookers_name 'Tester'
    rr.number_of_microphone 3
    rr.status 'waiting'
    rr.room_id 1
  end

  Factory.define :wrong_room_request do |wrr|
    wrr.starts_at ''
    wrr.ends_at ''
    wrr.request_type ''
    wrr.number_of_participants 0
    wrr.bookers_name 'r'
    wrr.number_of_microphone 0
    wrr.status ''
    wrr.room_id ''
  end

和我的room_request_spec.rb文件:

require 'spec_helper'

describe RoomRequest do

  it "accepts if room is selected" do
    room_request = Factory(:room_request)
    room_request.room_id.should_not be_nil
    room_request.should be_valid
  end

  it "rejects when room is not selected" do
    wrong_room_request = Factory(WrongRoomRequest)
    wrong_room_request.room_id.should be_nil
    wrong_room_request.should_not be_valid
  end

end

当我通过 rspec spec/models/room_request_spec 运行测试时.rb 我有:

Failures:

  1) RoomRequest rejects when room is not selected
     Failure/Error: wrong_room_request = Factory(WrongRoomRequest)
     NameError:
       uninitialized constant WrongRoomRequest
     # ./spec/models/room_request_spec.rb:12:in `block (2 levels) in <top (required)>'

我不知道如何初始化它,请帮忙:/

I'm using Rails 3.2.1 with factory_girl_rails and I've got a problem, here is my factories.rb file:

require factory_girl
  ##defining room requests
  Factory.define :room_request do |rr|
    rr.starts_at '2012-02-06 08:00:00 UTC'
    rr.ends_at '2012-02-06 17:00:00 UTC'
    rr.request_type 'normal request'
    rr.number_of_participants 5
    rr.bookers_name 'Tester'
    rr.number_of_microphone 3
    rr.status 'waiting'
    rr.room_id 1
  end

  Factory.define :wrong_room_request do |wrr|
    wrr.starts_at ''
    wrr.ends_at ''
    wrr.request_type ''
    wrr.number_of_participants 0
    wrr.bookers_name 'r'
    wrr.number_of_microphone 0
    wrr.status ''
    wrr.room_id ''
  end

And my room_request_spec.rb file:

require 'spec_helper'

describe RoomRequest do

  it "accepts if room is selected" do
    room_request = Factory(:room_request)
    room_request.room_id.should_not be_nil
    room_request.should be_valid
  end

  it "rejects when room is not selected" do
    wrong_room_request = Factory(WrongRoomRequest)
    wrong_room_request.room_id.should be_nil
    wrong_room_request.should_not be_valid
  end

end

When I'm running my test by rspec spec/models/room_request_spec.rb I've got:

Failures:

  1) RoomRequest rejects when room is not selected
     Failure/Error: wrong_room_request = Factory(WrongRoomRequest)
     NameError:
       uninitialized constant WrongRoomRequest
     # ./spec/models/room_request_spec.rb:12:in `block (2 levels) in <top (required)>'

I don't know how to initialize it, plz help :/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盗心人 2025-01-12 07:39:30

您需要使用符号而不是常量来调用工厂。

wrong_room_request = Factory(:wrong_room_request)

将您的 factories.rb 文件更新为新语法,如下所示:

FactoryGirl.define do
  factory :room_request do
    starts_at '2012-02-06 08:00:00 UTC'
    ends_at '2012-02-06 17:00:00 UTC'
    request_type 'normal request'
    number_of_participants 5
    bookers_name 'Tester'
    number_of_microphone 3
    status 'waiting'
    room_id 1
  end

  factory :wrong_room_request do
    starts_at ''
    ends_at ''
    request_type ''
    number_of_participants 0
    bookers_name 'r'
    number_of_microphone 0
    status ''
    room_id ''
  end
end

最后,我假设您正在使用factory_girl_rails gem。因此,为了确保其设置正确,您的gemfile应包括:

group :test do
  gem 'factory_girl_rails', :require => false
end

并且您的spec_helper.rb应包括

require 'factory_girl_rails' 

You need to call a Factory with a symbol, not a constant.

wrong_room_request = Factory(:wrong_room_request)

Update your factories.rb file to the new syntax as follows:

FactoryGirl.define do
  factory :room_request do
    starts_at '2012-02-06 08:00:00 UTC'
    ends_at '2012-02-06 17:00:00 UTC'
    request_type 'normal request'
    number_of_participants 5
    bookers_name 'Tester'
    number_of_microphone 3
    status 'waiting'
    room_id 1
  end

  factory :wrong_room_request do
    starts_at ''
    ends_at ''
    request_type ''
    number_of_participants 0
    bookers_name 'r'
    number_of_microphone 0
    status ''
    room_id ''
  end
end

Finally, I assume you are using the factory_girl_rails gem. So to make sure it is set up properly your gemfile should include:

group :test do
  gem 'factory_girl_rails', :require => false
end

And your spec_helper.rb should include

require 'factory_girl_rails' 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文