所以我在Michael Hartl(伟大的)RoR 教程的第 11 章。完成第 10 章后,我决定将微博形式添加到用户的个人资料页面,这样您也可以从那里发帖,而不仅仅是从主页发帖。
我设法让它工作得很好,但它以某种方式使我的测试套件崩溃......
这是我从 Rspec 获得的错误消息,用于我的所有 User_pages_spec.rb 配置文件页面测试:
UserPages profile page
Failure/Error: before { visit user_path(user) }
NoMethodError:
undefined method `microposts' for nil:NilClass
# ./app/controllers/users_controller.rb:17:in `show'
# ./spec/requests/user_pages_spec.rb:60:in `block (3 levels) in <top (required)>'
这是我的测试文件:
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
end
我的用户模型中确实有 microposts 方法:
has_many :microposts, dependent: :destroy
这是我在用户显示视图中添加的代码:
<% if current_user?(@user) %>
<section>
<%= render 'shared/micropost_form' %>
</section>
<% end %>
我还将 @microposts 变量添加到 UserController 中的显示操作中:
def show
@user = User.find(params[:id])
@microposts = @user.microposts.paginate(page: params[:page])
@micropost = current_user.microposts.build
end
So as everything seems to work out just fine, in development and in production, I don't understand why my tests won't pass... They passed before I added the micropost form in the user profile page...
If anybody can explain what the problem is, I would greatly appreciate the help!