2016-01-30 19 views
0

我收到一個非常奇怪的錯誤,因爲它在瀏覽器中完美工作,即顯示帖子並顯示作者姓名。沒有錯誤。未定義的方法爲零:RSpec中的NilClass,即使手動測試通過

但是,功能測試正在打破,我不知道爲什麼。這很奇怪。

posts_features_spec.rb: 

require 'spec_helper' 
require_relative '../support/spec_helper_methods' 

feature "Post Features" do 
    include SpecHelperMethods 
    let(:user){ User.create(name: "Andy", email: "[email protected]", password: "password", password_confirmation: "password") } 

    describe "Creating Posts" do 
    it "Can create a post" do 
     visit '/' 
     click_on "Sign Up" 
     fill_in 'user[name]', with: "Bob" 
     fill_in 'user[email]', with: "[email protected]" 
     fill_in 'user[password]', with: "password" 
     fill_in 'user[password_confirmation]', with: "password" 
     click_on 'Sign Up' 
     click_on 'Login' 
     fill_in 'email', with: "[email protected]" 
     fill_in 'password', with: "password" 
     click_on 'Login' 
     expect(page).to have_content "Successfully signed in!" 
     click_on "Create Post" 
     fill_in 'post[title]', with: "Title99" 
     fill_in 'post[body]', with: "Body99" 
     click_on "Post" 
     expect(current_path).to eq '/posts' 
     expect(page).to have_content "Title99" 
     expect(page).to have_content "Body99" 
     expect(page).to have_content "Bob" 
     expect(page).to have_content "Post Posted!" 
    end 
    end 

    describe 'Post Show page' do 
    let(:post){ Post.create title: 'Lazy-loaded Post', body: 'Body', user: user } 
    before(:each){ visit "/posts/show/#{post.id}" } 

    it "Post's title, body, and author are displayed" do 
     expect(page).to have_content "#{post.title}" 
     expect(page).to have_content "#{post.body}" 
     expect(page).to have_content "#{post.user.name}" 
    end 
    end 
end 

控制器:

PadrinoBlog::App.controllers :posts do 

    get :new do 
    @post = Post.new 
    render 'posts/new' 
    end 

    post :create do 
    post = Post.new(params[:post]) 
    post.user = current_user 
    if post.save 
     puts Post.all.count 
     puts Post.last.inspect 
     redirect('/posts', notice: "Post Posted!") 
    end 
    end 

    get :index do 
    @posts = Post.order('created_at DESC').all 
    render 'posts/index' 
    end 

    get :show, with: :id do 
    @post = Post.find_by_id(params[:id]) 
    render 'posts/show' 
    end 

end 

index.html.erb:

<%= link_to "Sign Up", 'users/new' %> 

<% unless signed_in? %> 
    <%= link_to "Login", 'sessions/new' %> 
<% end %> 

<% if signed_in? %> 
    <%= link_to "Logout", 'sessions/destroy' %> 
<% end %> 

<% if flash[:notice] %> 
    <p><%= flash[:notice] %></p> 
<% end %> 

<% if signed_in? %> 
    <%= link_to "Create Post", 'posts/new' %> 
<% end %> 

<% if @posts %> 
    <%= partial 'posts/post', collection: @posts %> 
<% end %> 

_posts.html.erb:

<div class='post'> 
    <h4><%= post.title %></h4> 
    <p><%= post.body %></p> 
    <p><%= post.user.name %></p> 
</div> 

在運行測試中,「展頁'測試通過,但第一個(長的)打破了wh恩它點擊「創建郵報」,有這樣的:

Failure/Error: <p><%= post.user.name %></p> 

    NoMethodError: 
     undefined method `name' for nil:NilClass 
    # ./app/views/posts/_post.html.erb:4:in `block in singleton class' 
    # ./app/views/posts/_post.html.erb:-7:in `instance_eval' 
    # ./app/views/posts/_post.html.erb:-7:in `singleton class' 
    # ./app/views/posts/_post.html.erb:-9:in `__tilt_5665000' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/tilt-1.4.1/lib/tilt/template.rb:170:in `call' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/tilt-1.4.1/lib/tilt/template.rb:170:in `evaluate' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/tilt-1.4.1/lib/tilt/template.rb:103:in `render' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/padrino-helpers-0.13.1/lib/padrino/rendering/erb_template.rb:18:in `render' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/sinatra-1.4.6/lib/sinatra/base.rb:822:in `render' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/padrino-helpers-0.13.1/lib/padrino/rendering.rb:212:in `render' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/padrino-helpers-0.13.1/lib/padrino-helpers/render_helpers.rb:55:in `block in partial' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5/lib/active_record/relation/delegation.rb:132:in `each' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5/lib/active_record/relation/delegation.rb:132:in `each_with_object' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5/lib/active_record/relation/delegation.rb:132:in `public_send' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5/lib/active_record/relation/delegation.rb:132:in `method_missing' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5/lib/active_record/relation/delegation.rb:99:in `method_missing' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/padrino-helpers-0.13.1/lib/padrino-helpers/render_helpers.rb:48:in `partial' 
    # ./app/views/posts/index.html.erb:20:in `block in singleton class' 
    # ./app/views/posts/index.html.erb:-6:in `instance_eval' 
    # ./app/views/posts/index.html.erb:-6:in `singleton class' 
    # ./app/views/posts/index.html.erb:-8:in `__tilt_5665000' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/tilt-1.4.1/lib/tilt/template.rb:170:in `call' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/tilt-1.4.1/lib/tilt/template.rb:170:in `evaluate' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/tilt-1.4.1/lib/tilt/template.rb:103:in `render' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/padrino-helpers-0.13.1/lib/padrino/rendering/erb_template.rb:18:in `render' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/sinatra-1.4.6/lib/sinatra/base.rb:822:in `render' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/padrino-helpers-0.13.1/lib/padrino/rendering.rb:212:in `render' 
    # ./app/controllers/posts.rb:20:in `block (2 levels) in <top (required)>' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/padrino-core-0.13.1/lib/padrino-core/application/routing.rb:517:in `call' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/padrino-core-0.13.1/lib/padrino-core/application/routing.rb:517:in `block in route' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/padrino-core-0.13.1/lib/padrino-core/application/routing.rb:992:in `block in invoke_route' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/padrino-core-0.13.1/lib/padrino-core/application/routing.rb:987:in `catch' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/padrino-core-0.13.1/lib/padrino-core/application/routing.rb:987:in `invoke_route' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/padrino-core-0.13.1/lib/padrino-core/application/routing.rb:952:in `block in route!' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/padrino-core-0.13.1/lib/padrino-core/path_router/compiler.rb:53:in `block in call_by_request' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/padrino-core-0.13.1/lib/padrino-core/path_router/compiler.rb:84:in `block in rotation' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/padrino-core-0.13.1/lib/padrino-core/path_router/compiler.rb:83:in `loop' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/padrino-core-0.13.1/lib/padrino-core/path_router/compiler.rb:83:in `with_object' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/padrino-core-0.13.1/lib/padrino-core/path_router/compiler.rb:83:in `rotation' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/padrino-core-0.13.1/lib/padrino-core/path_router/compiler.rb:49:in `call_by_request' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/padrino-core-0.13.1/lib/padrino-core/path_router.rb:43:in `call' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/padrino-core-0.13.1/lib/padrino-core/application/routing.rb:949:in `route!' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/padrino-core-0.13.1/lib/padrino-core/application/routing.rb:932:in `block in dispatch!' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/sinatra-1.4.6/lib/sinatra/base.rb:1066:in `block in invoke' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/sinatra-1.4.6/lib/sinatra/base.rb:1066:in `catch' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/sinatra-1.4.6/lib/sinatra/base.rb:1066:in `invoke' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/padrino-core-0.13.1/lib/padrino-core/application/routing.rb:930:in `dispatch!' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/sinatra-1.4.6/lib/sinatra/base.rb:906:in `block in call!' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/sinatra-1.4.6/lib/sinatra/base.rb:1066:in `block in invoke' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/sinatra-1.4.6/lib/sinatra/base.rb:1066:in `catch' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/sinatra-1.4.6/lib/sinatra/base.rb:1066:in `invoke' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/sinatra-1.4.6/lib/sinatra/base.rb:906:in `call!' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/rspec-padrino-0.2.2/lib/rspec/padrino/initializers/last_application.rb:12:in `block (2 levels) in <top (required)>' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/sinatra-1.4.6/lib/sinatra/base.rb:894:in `call' 
    # ./lib/connection_pool_management_middleware.rb:7:in `block in call' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:in `with_connection' 
    # ./lib/connection_pool_management_middleware.rb:7:in `call' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/sass-3.4.21/lib/sass/plugin/rack.rb:54:in `call' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/rack-protection-1.5.3/lib/rack/protection/base.rb:49:in `call' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/rack-protection-1.5.3/lib/rack/protection/xss_header.rb:18:in `call' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/rack-protection-1.5.3/lib/rack/protection/base.rb:49:in `call' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/rack-protection-1.5.3/lib/rack/protection/base.rb:49:in `call' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/rack-protection-1.5.3/lib/rack/protection/json_csrf.rb:18:in `call' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/rack-protection-1.5.3/lib/rack/protection/base.rb:49:in `call' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/rack-protection-1.5.3/lib/rack/protection/base.rb:49:in `call' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/rack-protection-1.5.3/lib/rack/protection/frame_options.rb:31:in `call' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/rack-1.6.4/lib/rack/head.rb:13:in `call' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/rack-1.6.4/lib/rack/methodoverride.rb:22:in `call' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/rack-1.6.4/lib/rack/session/abstract/id.rb:225:in `context' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/rack-1.6.4/lib/rack/session/abstract/id.rb:220:in `call' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/sinatra-1.4.6/lib/sinatra/base.rb:2021:in `call' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/sinatra-1.4.6/lib/sinatra/base.rb:1486:in `block in call' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/sinatra-1.4.6/lib/sinatra/base.rb:1795:in `synchronize' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/sinatra-1.4.6/lib/sinatra/base.rb:1486:in `call' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/padrino-core-0.13.1/lib/padrino-core/router.rb:84:in `block in call' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/padrino-core-0.13.1/lib/padrino-core/router.rb:75:in `each' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/padrino-core-0.13.1/lib/padrino-core/router.rb:75:in `call' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/rack-test-0.6.3/lib/rack/mock_session.rb:30:in `request' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/rspec-padrino-0.2.2/lib/rspec/padrino/initializers/last_application.rb:34:in `block (2 levels) in <top (required)>' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/rack-test-0.6.3/lib/rack/test.rb:244:in `process_request' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/rack-test-0.6.3/lib/rack/test.rb:58:in `get' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/capybara-2.6.0/lib/capybara/rack_test/browser.rb:60:in `process' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/capybara-2.6.0/lib/capybara/rack_test/browser.rb:38:in `block in process_and_follow_redirects' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/capybara-2.6.0/lib/capybara/rack_test/browser.rb:37:in `times' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/capybara-2.6.0/lib/capybara/rack_test/browser.rb:37:in `process_and_follow_redirects' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/capybara-2.6.0/lib/capybara/rack_test/browser.rb:26:in `submit' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/capybara-2.6.0/lib/capybara/rack_test/form.rb:77:in `submit' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/capybara-2.6.0/lib/capybara/rack_test/node.rb:61:in `click' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/capybara-2.6.0/lib/capybara/node/element.rb:134:in `block in click' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/capybara-2.6.0/lib/capybara/node/base.rb:84:in `synchronize' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/capybara-2.6.0/lib/capybara/node/element.rb:134:in `click' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/capybara-2.6.0/lib/capybara/node/actions.rb:13:in `click_link_or_button' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/capybara-2.6.0/lib/capybara/session.rb:686:in `block (2 levels) in <class:Session>' 
    # /home/andrew/.rvm/gems/ruby-2.3.0/gems/capybara-2.6.0/lib/capybara/dsl.rb:51:in `block (2 levels) in <module:DSL>' 
    # ./spec/features/posts_features_spec.rb:25:in `block (3 levels) in <top (required)>' 
    # ./spec/spec_helper.rb:17:in `block (2 levels) in <top (required)>' 

就像我說的,在瀏覽器中,沒有休息本地運行沒有問題,但這個測試斷裂,這是非常奇怪的,特別是因爲放映視圖(其不破)是:

<h4><%= @post.title %></h4> 
<p><%= @post.body %></p> 
<p><%= @post.user.name %></p> 

我甚至試圖做的事情,而不在索引視圖中的部分,以:

<% @posts.each do |post| %> 
    <h4><%= post.title %></h4> 
    <p><%= post.body %></p> 
    <p><%= post.user.name %></p> 
<% end %> 

...但我得到完全相同的錯誤。 Wtf正在發生?

我推這個當前的狀態提交here

+1

檢查'post.user'是零 – sbs

+0

檢查零將有助於避免一個錯誤。我建議在'創建'點擊前查看一個[截圖](https://github.com/mattheworiordan/capybara-screenshot),看看你的應用程序處於什麼狀態。可能數據沒有加載(IE'在數據被髮送回去之前,用戶在一秒鐘內是零)。 – dannypaz

+0

將post.user放在控制器中返回用戶。 – Andy

回答

1

檢查你的代碼並運行測試我不明白的錯誤,讓我在你的遷移剛剛看了一下,最後遷移增加用戶到帖子。我猜你有數據庫的問題在測試環境中

padrino rake ar:migrate:reset -e test 

應該重置DB並修復問題

+0

我認爲Tom可能是對的,但是想補充說應該在您的Gemfile中添加pry-debugger或byebug,並且毫不猶豫地拋出一個接近錯誤源的調試器行。如果它確實是一個缺失的列,那麼在規範執行時從調試器中檢查Post.column_names會讓你失望。 – johncip

+0

我已經很確定我已經爲測試數據庫運行了遷移,但是我沒有改變就運行了你的命令,並且還運行了'padrino rake ar:migrate -e test',只是爲了加倍確定,並且仍然沒有改變。我也跑過了web,並且'Post'好像被'user_id'持久化到了數據庫。另外,在測試過程中,byebug中的Post.column_names也顯示一個'user_id'列。絕對奇怪的是,它運行在你的機器上沒有錯誤!這到底是怎麼回事? – Andy

+0

固定。它在你的機器上運行的事實是線索。我重置測試數據庫,並解決了這個問題。感謝大家。湯姆,如果你編輯你的答案,包括運行'padrino rake ar:migrate:reset -e test'(這解決了我的問題),我會給你最好的答案。看起來很遺憾,當你離解決方案非常近時,剝奪了你的這些點數;-) – Andy

相關問題