使用 Mongoid、Rspec 的未定义方法 `reflect_on_association`
我正在学习一些 Rails!但现在,我似乎无法克服 RSpec 抛出的错误。错误如下:
1) EntryMethodsController POST create with valid params creates a new EntryMethod Failure/Error: post :create, NoMethodError: undefined method `reflect_on_association' for "4e94ca4f66472f02ff00000a":String # ./app/controllers/entry_methods_controller.rb:43:in `create' # ./spec/controllers/entry_methods_controller_spec.rb:48:in `block (4 levels) in ' Finished in 0.29409 seconds 13 examples, 1 failure Failed examples: rspec ./spec/controllers/entry_methods_controller_spec.rb:47 # EntryMethodsController POST create with valid params creates a new EntryMethod Done.
测试
describe "POST create" do
describe "with valid params" do
before :each do
@contest = FactoryGirl.create(:contest)
end
after :each do
@contest.destroy
end
it "creates a new EntryMethod" do
expect {
post :create,
:contest => @contest,
:entry_method => FactoryGirl.attributes_for(:url_entry_method, :contest => @contest)
}.to change(@contest.entry_methods, :count).by(1)
end
end
end
控制器
def create
@entry_method = Contest.find(params[:contest_id])
.entry_methods.new(params[:entry_method])
respond_to do |format|
if @entry_method.save
format.html { redirect_to @entry_method, notice: 'Entry method was successfully created.' }
format.json { render json: @entry_method, status: :created, location: @entry_method }
else
format.html { render action: "new" }
format.json { render json: @entry_method.errors, status: :unprocessable_entity }
end
end
end
模型
class Contest
include Mongoid::Document
include Mongoid::Timestamps
field :name, :type => String
field :rules, :type => String
field :start_date, :type => Time
field :end_date, :type => Time
embeds_many :entry_methods
end
class EntryMethod
include Mongoid::Document
field :url, :type => String
field :string, :type => String
embedded_in :contest
end
谢谢,太棒了。 :)
I'm learning me some Rails! But right now, I can't seem to get past an error RSpec is throwing. The error is as follows:
1) EntryMethodsController POST create with valid params creates a new EntryMethod Failure/Error: post :create, NoMethodError: undefined method `reflect_on_association' for "4e94ca4f66472f02ff00000a":String # ./app/controllers/entry_methods_controller.rb:43:in `create' # ./spec/controllers/entry_methods_controller_spec.rb:48:in `block (4 levels) in ' Finished in 0.29409 seconds 13 examples, 1 failure Failed examples: rspec ./spec/controllers/entry_methods_controller_spec.rb:47 # EntryMethodsController POST create with valid params creates a new EntryMethod Done.
Test
describe "POST create" do
describe "with valid params" do
before :each do
@contest = FactoryGirl.create(:contest)
end
after :each do
@contest.destroy
end
it "creates a new EntryMethod" do
expect {
post :create,
:contest => @contest,
:entry_method => FactoryGirl.attributes_for(:url_entry_method, :contest => @contest)
}.to change(@contest.entry_methods, :count).by(1)
end
end
end
Controller
def create
@entry_method = Contest.find(params[:contest_id])
.entry_methods.new(params[:entry_method])
respond_to do |format|
if @entry_method.save
format.html { redirect_to @entry_method, notice: 'Entry method was successfully created.' }
format.json { render json: @entry_method, status: :created, location: @entry_method }
else
format.html { render action: "new" }
format.json { render json: @entry_method.errors, status: :unprocessable_entity }
end
end
end
Models
class Contest
include Mongoid::Document
include Mongoid::Timestamps
field :name, :type => String
field :rules, :type => String
field :start_date, :type => Time
field :end_date, :type => Time
embeds_many :entry_methods
end
class EntryMethod
include Mongoid::Document
field :url, :type => String
field :string, :type => String
embedded_in :contest
end
Thanks, SO awesome people. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这是因为您将完整的
@contest
对象传递给create
操作的参数,而实际上它需要一个Hash< /code> 属性。
您可以通过将对该操作的调用更改为以下内容来解决此问题:
我也不会让 FactoryGirl 为您创建对象,因为这可能会导致唯一性验证或任何可能失败的情况。您应该使用
FactoryGirl.build
,而不是FactoryGirl.create
。I reckon this is because you're passing through a full
@contest
object to the parameters of thecreate
action, when it's actually going to be expecting aHash
of attributes.You can fix this by changing your call to that action to this:
I would also not get FactoryGirl to create the object for you, as that may lead to uniqueness validations or whatever possibly failing. You should be using
FactoryGirl.build
, and notFactoryGirl.create
.