我一直在关注Michael Hartl的Rails教程。实际上我已经完成了它,但是我在最后一章的最后一次练习中遇到了一些重构问题。我所做的只是改变用户模型的show视图(show.html.erb):
<section>
<h1>
<%= gravatar_for @user %>
<%= @user.name %>
</h1>
</section>
对此:
<section>
<%= render 'shared/user_info' %>
</section>
在部分(_user_info.html.erb)中我有这个:
<a href="<%= user_path(current_user) %>">
<%= gravatar_for current_user, size: 52 %>
</a>
<h1> <%= current_user.name %> </h1>
<% unless current_page?(user_path(current_user)) %>
<span> <%= link_to "view my profile", current_user %> </span>
<span> <%= pluralize(current_user.microposts.count, "micropost") %> </span>
<% end %>
浏览器上的一切正常,但由于某些原因rspec失败了一些测试,我怀疑rspec在调用sessions_helper.rb中定义的current_user方法时遇到问题,顺便提一下,它包含在application_helper.rb中。 gravatar_for函数在users_helper.rb中定义。这是我从测试中得到的错误:
失败/错误:{visit user_path(user)}之前
::的ActionView ::模板错误:
未定义的方法email' for nil:NilClass
# ./app/helpers/users_helper.rb:4:in
gravatar_for'
#。/ app/views/shared/_user_info.html.erb:1:in _app_views_shared__user_info_html_erb___3480157814439046731_47327400'
# ./app/views/users/show.html.erb:5:in
_ app_views_users_show_html_erb___1252254778347368838_47378900'
'./spec/requests/user_pages_spec.rb:58:in'块(3级)in'
如果您能帮助我确定这里发生了什么,我将不胜感激。我可以找到不同的方法来做同样的事情,但我对此非常好奇。我对Rails不是很有经验,这就是我遵循教程的原因,如果我遗漏了一些明显的东西,请原谅我。感谢。
答案 0 :(得分:3)
我想我得到了解决方案。虽然我仍然感到困惑。以下是我在问题中提到的重构之前的测试代码(因为它们在教程中,它们都在传递):
describe "profile page" do
let(:user) { FactoryGirl.create(:user) }
let!(:m1) { FactoryGirl.create(:micropost, user: user, content: "Foo") }
let!(:m2) { FactoryGirl.create(:micropost, user: user, content: "Bar") }
before { visit user_path(user) }
it { should have_selector('h1', text: user.name) }
it { should have_selector('title', text: user.name) }
describe "microposts" do
it { should have_content(m1.content) }
it { should have_content(m2.content) }
it { should have_content(user.microposts.count) }
end #some other tests for show page continue, also having the same behaviour
end
在阅读测试以查看是否存在错误时,我开始想知道为什么那些测试在我没有签署用户的情况下通过,所以我添加了代码以在之前的块中登录用户,如下所示: / p>
describe "profile page" do
let(:user) { FactoryGirl.create(:user) }
let!(:m1) { FactoryGirl.create(:micropost, user: user, content: "Foo") }
let!(:m2) { FactoryGirl.create(:micropost, user: user, content: "Bar") }
before do
valid_signin user
visit user_path(user)
end
it { should have_selector('h1', text: user.name) }
it { should have_selector('title', text: user.name) }
describe "microposts" do
it { should have_content(m1.content) }
it { should have_content(m2.content) }
it { should have_content(user.microposts.count) }
end #some other tests for show page continue, also having the same behaviour
end
现在所有测试都通过了。我知道签署用户似乎很愚蠢和明显,但是the tutorial中的测试是如何进行的。如果要检查,Here是更新的测试文件。所有更改现在都在我的github仓库中提交。谢谢大家的帮助。
答案 1 :(得分:2)
围绕your repo,这个可能成了问题:6月25日在Rails教程中报告了一个关于current_user
的设置的小错误 app / helpers / sessions_helper.rb 文件中的sign_in
和sign_out
方法。该通知似乎未在Rails Tutorial News site上发布,但已发送给订阅者,并且会反映在current online book说要更改:
self.current_user = user # in the sign_in method
self.current_user = nil # in the sign_out method
因此,请尝试更新your sessions_helper.rb
,看看是否会阻止您获得nil
用户。
答案 2 :(得分:1)
听起来您的测试数据库中没有任何用户。开发数据库和测试数据库是两回事。 Rspec在运行测试时使用测试数据库。
要设置测试数据库,请执行rake db:test:prepare
。
然后,您需要在运行规范时填充测试数据库。一个很好的方法是使用Factory Girl gem。