Rspec / Rails 4:失败/错误:访问" / todos" NoMethodError:未定义的方法`todos'对于nil:rspec测试中的NilClass

时间:2014-12-08 06:55:37

标签: ruby-on-rails ruby-on-rails-4 rspec rspec-rails

无法完成测试通过。有人可以指出我缺少什么。 我正在测试todos的索引页面(这是单页面应用程序)。

错误日志:

1) Displaying todos should display the list of todos
 Failure/Error: visit "/todos"
 NoMethodError:
   undefined method `todos' for nil:NilClass
 # ./app/controllers/todos_controller.rb:6:in `index'
 # ./spec/features/todos/index_spec.rb:24:in `block (2 levels) in <top (required)>'

todos_controller.rb

 class TodosController < ApplicationController

skip_before_action :authenticate_user!

 def index
 @todos_completed_today = current_user.todos.where(is_completed: true).
 where("updated_at  >= ?", (Time.zone.now - 24.hours)).order('todos.position ASC')

 @todos = current_user.todos.where(is_completed: false).order('todos.position ASC'
 end

 def create
  @todo = current_user.todos.new(todo_params)
  @todo.save
   respond_to do |format|
    format.html { redirect_to todos_path }
    format.js
  end
 end

 def edit
  @todo = current_user.todos.find_by_id(params[:id])
 end

 def update
  @todo = current_user.todos.find_by_id(params[:id])
  @todo.update_attributes(todo_params)
 end

这是我的待办事项索引规范。 特征/待办事项/ index_spec.rb

 describe "Displaying todos", :js => true do
   let(:todo) { FactoryGirl.create(:todo)}

    it "should display the list of todos" do

      visit "/todos"

     expect(Todo.count).to eq(0)
    page.should have_selector('h1', :text => 'Todos')


    fill_in "title", with: "MyString"
    click_button "Create"

   expect(page).to have_content("Todo has been created!")
   expect(todo.todo_title).to eq(title)

  todo.reload!
  expect(Todo.count).to eq(1)
  expect(todo.title).eq("Mystring")
 end

工厂/ todo.rb

# Read about factories at https://github.com/thoughtbot/factory_girl

  FactoryGirl.define do
    factory :todo do
    title "MyString"
    description "MyText"
    is_completed false
    position 1
   end
 end

1 个答案:

答案 0 :(得分:1)

规格/支持/ devise.rb

module FeatureMacros
  def login_user_in_feature
    Warden.test_reset!
    Warden.test_mode!
    user = create(:user)
    login_as user, :scope => :user
    user.confirmed_at = Time.now
    user.confirm!
    user.save
    user
  end
end

RSpec.configure do |config|
  config.include Devise::TestHelpers, :type => :controller
  config.include Devise::TestHelpers, :type => :view
  config.include Warden::Test::Helpers
  config.include FeatureMacros
end

在factories / user.rb

FactoryGirl.define do
  factory :user do
    #your user factory
  end
end

在工厂/ todo.rb

FactoryGirl.define do
  factory :todo do
    title "MyString"
    description "MyText"
    is_completed false
    position 1
    user{User.last}
  end
end

在features / todos / index_spec.rb

feature "Displaying todos", :js => true do
  scenario "should display the list of todos" do
    login_user_in_feature
    todo = FactoryGirl.create(:todo)
    visit "/todos"
    expect(Todo.count).to eq(0)
    page.should have_selector('h1', :text => 'Todos')
    fill_in "title", with: "MyString"
    click_button "Create"
    expect(page).to have_content("Todo has been created!")
    expect(todo.todo_title).to eq(title)
    todo.reload!
    expect(Todo.count).to eq(1)
    expect(todo.title).eq("Mystring")
  end
end

如果它不起作用或有任何东西,请告诉我,您可以使用FeatureMacros查看用户的身份验证,因为current_user在没有它的情况下无法工作