测试时来自factory_girls的未初始化常量
我将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用符号而不是常量来调用工厂。
将您的
factories.rb
文件更新为新语法,如下所示:最后,我假设您正在使用factory_girl_rails gem。因此,为了确保其设置正确,您的gemfile应包括:
并且您的spec_helper.rb应包括
You need to call a Factory with a symbol, not a constant.
Update your
factories.rb
file to the new syntax as follows:Finally, I assume you are using the factory_girl_rails gem. So to make sure it is set up properly your
gemfile
should include:And your
spec_helper.rb
should include