使用 Mongoid、Rspec 的未定义方法 `reflect_on_association`

发布于 2024-12-09 11:41:49 字数 2216 浏览 0 评论 0原文

我正在学习一些 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 技术交流群。

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

发布评论

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

评论(1

又怨 2024-12-16 11:41:49

我认为这是因为您将完整的 @contest 对象传递给 create 操作的参数,而实际上它需要一个 Hash< /code> 属性。

您可以通过将对该操作的调用更改为以下内容来解决此问题:

post :create,
     :contest => @contest.attributes,
     ...

我也不会让 FactoryGirl 为您创建对象,因为这可能会导致唯一性验证或任何可能失败的情况。您应该使用 FactoryGirl.build,而不是 FactoryGirl.create

I reckon this is because you're passing through a full @contest object to the parameters of the create action, when it's actually going to be expecting a Hash of attributes.

You can fix this by changing your call to that action to this:

post :create,
     :contest => @contest.attributes,
     ...

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 not FactoryGirl.create.

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