简体   繁体   English

Rspec / Rails 4:失败/错误:访问“ / todos” NoMethodError:在rspec测试中针对nil:NilClass的未定义方法“ todos”

[英]Rspec/Rails 4: Failure/Error: visit “/todos” NoMethodError: undefined method `todos' for nil:NilClass in rspec test

Failed to make my test pass. 无法通过我的测试。 Can someone pointme what am missing. 有人可以指出缺少的内容吗? I am testing the index page of the todos(which is single page application). 我正在测试待办事项的索引页(这是单页应用程序)。

Error Log: 错误日志:

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 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

Here is my todo index spec. 这是我的待办事项索引规格。 features/todos/index_spec.rb 特征/待办事项/ 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

factories/todo.rb 工厂/ 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

spec/support/devise.rb 规格/支持/ 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

In factories/user.rb 在factory / user.rb中

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

In factories/todo.rb 在factory / todo.rb中

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

In features/todos/index_spec.rb 在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

If it did not work or have anything, please tell me, you can see the authentication for the user using FeatureMacros because current_user won't work without it 如果它不起作用或没有任何帮助,请告诉我,您可以使用FeatureMacros看到用户的身份验证,因为没有它,current_user将无法工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rspec/Rails3:NoMethodError:nil:NilClass 的未定义方法“文本” - Rspec/Rails3: NoMethodError: undefined method `text' for nil:NilClass RSpec失败:nil:NilClass的未定义方法“名称” - RSpec failure: undefined method `name' for nil:NilClass nil:NilClass rspec测试的未定义方法“ response” - undefined method `response' for nil:NilClass rspec test nil的未定义方法`save&#39;:RSpec测试中的NilClass - Undefined method `save' for nil:NilClass in RSpec test RSpec Rails中nil:NilClass的未定义方法&#39;session&#39; - undefined method `session' for nil:NilClass in RSpec Rails rspec控制器测试错误nil:NilClass的未定义方法 - rspec controller test error undefined method for nil:NilClass NoMethodError:#的未定义方法“访问” - NoMethodError: undefined method `visit' for #<RSpec:: Rspec测试:NoMethodError-nil.Nilclass的未定义方法“ id” - Rspec Testing : NoMethodError - undefined method 'id' for nil.Nilclass NoMethodError:RSpec 中 nil:NilClass 的未定义方法“customer_details” - NoMethodError: undefined method 'customer_details' for nil:NilClass in RSpec ActionView::Template::Error: 使用 rspec rails 进行集成测试时 nil:NilClass 的未定义方法`image_url&#39; - ActionView::Template::Error: undefined method `image_url' for nil:NilClass when doing integration test with rspec rails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM