用于#<model:0x000 ...> </model:0x000 ...>的未定义方法'NameOfField'

时间:2011-11-06 09:09:58

标签: ruby-on-rails-3 rspec rspec-rails

我完全被这个错误所困扰。非常感谢一些帮助:)。

要重现错误,您可以从https://github.com/WaleyChen/twitter_clone提取程序。然后运行'bundle exec rspec spec /'.

我的控制器的rspec测试定义为:

require 'spec_helper'

describe FrontpageController do
  render_views # render the views inside the controller tests, so not just test the actions

  describe "GET 'frontpage'" do
    it "should be successful" do
      get 'frontpage'
      response.should be_success
    end

    it "should have the right title" do
      get 'frontpage'
      response.should have_selector("title", :content => "Twitter")
    end
  end

end

当我运行我的rspec测试时,我收到以下错误:

Failures:

1) FrontpageController GET 'frontpage' should be successful
 Failure/Error: get 'frontpage'
 ActionView::Template::Error:
   undefined method `full_name' for #<User:0x007fbfce43dce0>
 # ./app/views/frontpage/frontpage.html.erb:22:in `block in _app_views_frontpage_frontpage_html_erb___4518234645475110659_70230885952360'
 # ./app/views/frontpage/frontpage.html.erb:21:in `_app_views_frontpage_frontpage_html_erb___4518234645475110659_70230885952360'
 # ./spec/controllers/frontpage_controller_spec.rb:8:in `block (3 levels) in <top (required)>'

2) FrontpageController GET 'frontpage' should have the right title
 Failure/Error: get 'frontpage'
 ActionView::Template::Error:
   undefined method `full_name' for #<User:0x007fbfcc99a410>
 # ./app/views/frontpage/frontpage.html.erb:22:in `block in _app_views_frontpage_frontpage_html_erb___4518234645475110659_70230885952360'
 # ./app/views/frontpage/frontpage.html.erb:21:in `_app_views_frontpage_frontpage_html_erb___4518234645475110659_70230885952360'
 # ./spec/controllers/frontpage_controller_spec.rb:13:in `block (3 levels) in <top (required)>'

这是控制器:

class FrontpageController < ApplicationController
  def frontpage
    @user = User.new
    @sign_up = User.new
  end

  def sign_up
    if @post.save
      format.html { redirect_to @post, notice: 'Post was successfully created.' }
    end
  end
end

以下是导致错误的视图:

<%= form_for @user, :url =>"sign_up" do |form| %>
  <%= form.text_field :full_name, :placeholder => "Full name" %>
  </br>
  <%= form.text_field :email, :placeholder => "Email" %>
  </br>
  <%= form.text_field :pw, :placeholder => "Password" %>
  </br>
  <%= form.text_field :username, :placeholder => "Username" %>
  </br>
  <%= form.submit "Sign up" %>
<% end %>

这是user.rb:

class User < ActiveRecord::Base
end

这是schema.rb:

ActiveRecord::Schema.define(:version => 20111106084309) do

  create_table "users", :force => true do |t|
    t.string   "full_name"
    t.string   "email"
    t.string   "username"
    t.string   "pw"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

end

1 个答案:

答案 0 :(得分:1)

您需要确保所有迁移的测试数据库都是最新的:

rake db:test:prepare

但是,当您的迁移被破坏时,您可能会遇到问题;你有两个名为“CreateUsers”的迁移,Rails会抱怨它们。您似乎应删除较新的一行,然后取消注释原始行中的t.string :email行。

此外,如果您使用bundle exec rake rspec而不是bundle exec rspec spec,那么在运行测试之前,请确保您的数据库迁移是最新的。我刚刚克隆了你的回购并做了这件事,它通过了测试就好了。