1
我有一個用戶和郵政模型。而我展示在用戶視圖顯示在用戶的帖子:當我向will_paginate中添加「order」參數時,Rspec拋出瘋狂錯誤?
users_controller.rb:
def show
@user = User.find(params[:id])
default_order = "created_at DESC"
params[:order_by] ||= default_order
@posts = @user.posts.paginate(page: params[:page],
per_page: 10,
order: params[:order_by]) if signed_in?
@posts = @user.posts.paginate(page: params[:page])
end
用戶/ show.html.erb:
<% if @user.posts.any? %>
<h3>Posts (<%= @user.posts.count %>)</h3>
<ul class="posts unstyled">
<%= render @posts %>
</ul>
<%= will_paginate @posts %>
<% end %>
的帖子在現場表現很好。但是,當我運行這個規格:
user_pages_spec.rb:
describe "profile page" do
let(:user) { FactoryGirl.create(:user) }
let!(:p1) { FactoryGirl.create(:post, user: user, title: "Fo",
content: "Foo") }
let!(:p2) { FactoryGirl.create(:post, user: user, title: "Ba",
content: "Bar") }
before { visit user_path(user) }
it { should have_selector('h1', text: user.name) }
it { should have_selector('title', text: user.name) }
describe "posts" do
it { should have_content(p1.title) }
it { should have_content(p1.content) }
it { should have_content(p2.title) }
it { should have_content(p2.content) }
it { should have_content(user.posts.count) }
end
end
我得到這個瘋狂的錯誤:
6) User pages profile page posts
Failure/Error: it { should have_content(p1.title) }
expected there to be content "Fo" in "Action Controller: Exception caught
body { background-color: #fff; color: #333; }
body, p, ol, ul, td {
font-family: helvetica, verdana, arial, sans-serif;
font-size: 13px;
line-height: 18px;
}
pre {
background-color: #eee;
padding: 10px;
font-size: 11px;
white-space: pre-wrap;
}
a { color: #000; }
a:visited { color: #666; }
a:hover { color: #fff; background-color:#000; }
ArgumentError in
Users#show
Showing /home/alex/rails/inkleak/app/views/users/show.html.erb where line #20 raised:
'nil' is not an ActiveModel-compatible object that returns a valid partial path.
Extracted source (around line #20):
17: <h3>Posts (<%= @user.posts.count %>)
,去,去,去......像千行。
這很奇怪,因爲測試,如果我評論這行工作得很好:
#default_order = "created_at DESC"
#params[:order_by] ||= default_order
#order: params[:order_by])
可能是什麼問題呢?
編輯:
這裏是我的工廠:
factories.rb:
factory :user do
sequence(:name) { |n| "Person #{n}" }
sequence(:email) { |n| "person_#{n}@example.com"}
password "foobar"
password_confirmation "foobar"
factory :admin do
admin true
end
end
factory :post do
title "lorem"
content "lorem ipsum"
#tagging
category
user
after(:build) do |post|
post.tags << FactoryGirl.build(:tag)
end
end
你能證明錯誤的線#20你貼?這就是它說錯誤的地方。 –
@LeoCorrea這是用戶顯示視圖中的第20行:'<%= render @posts%>'那是你的意思? – alexchenco
你有工廠定義的標籤嗎?它可能會抱怨。在'post.tags << FactoryGirl.build(:標籤) –