有一个名为课程的模型,用户可以参加,因此我拥有@user.courses
- 它适用于 static-pages-home 的视图,因为它允许我通过下面列出的部分列出课程。但是,结果我在 rspec 测试中遇到了错误。
控制器
class StaticPagesController < ApplicationController
def home
@user = current_user
@courses = @user.courses
end
end
Rspec 错误
1) Static pages should have the right links on the layout
Failure/Error: visit root_path
NoMethodError:
undefined method `courses' for nil:NilClass
# ./app/controllers/static_pages_controller.rb:4:in `home'
# ./spec/requests/static_pages_spec.rb:55:in `block (2 levels) in <top (required)>'
Rspec 规范
require 'spec_helper'
describe "Static pages" do
subject { page }
describe "Home page" do
before { visit root_path} <<<error here (rb:55)
end
end
查看部分(有效)
<div class="row">
<div class="span8">
<% if @courses.any? %>
<ul class="courses">
<%- @courses.each do |course| %>
<%= link_to course.title, course_path(course) %>
<%- end%>
</ul>
<% end %>
</div>
</div>