0

当我运行 RSpec 时,我收到 4 个错误,全部说明:

undefined local variable or method `signin_path' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x4203fa8>

但是我正在运行其他使用这个助手和路径的测试,没有问题,它只是在关系控制器测试中不能使用它。

关系控制器规范.rb

require 'spec_helper'

describe RelationshipsController do

    let(:user) { FactoryGirl.create(:user) }
    let(:other_user) { FactoryGirl.create(:user) }

    before { sign_in user }

    describe "creating a relationship with Ajax" do

        it "should increment the Relationship count" do
            expect do
                xhr :post, :create, relationship: { followed_id: other_user.id }
            end.to change(Relationship, :count).by(1)
        end

        it "should respond with success" do
            xhr :post, :create, relationship: { followed_id: other_user.id }
            response.should be_success
        end
    end

    describe "destroying a relationship with Ajax" do

        before { user.follow!(other_user) }
        let(:relationship) { user.relationships.find_by_followed_id(other_user) }

        it "should decrement the Relationship count" do
            expect do
                xhr :delete, :destroy, id: relationship.id
            end.to change(Relationship, :count).by(-1)
        end

        it "should respond with success" do
            xhr :delete, :destroy, id: relationship.id
            response.should be_success
        end
    end
end

实用程序.rb:

def sign_in(user)
    visit signin_path
    fill_in 'Email',        with: user.email.upcase
    fill_in 'Password', with: user.password
    click_button 'Sign In'
    cookies[:remember_token] = user.remember_token
end

路线.rb:

  resources :users do
    member do
      get :following, :followers
    end
  end

  resources :sessions,      only: [:create, :destroy, :new]
  resources :microposts,    only: [:create, :destroy]
  resources :relationships, only: [:create, :destroy]

  # Application Root
  root to: 'static_pages#home'

  # Static Pages
  match '/help', to: 'static_pages#help'
  match '/about', to: 'static_pages#about'
  match '/contact', to: 'static_pages#contact'

  # Users
  match '/signup',  to: 'users#new'
  match '/signin',  to: 'sessions#new'
  match '/signout', to: 'sessions#destroy', via: :delete
4

1 回答 1

1

控制器规范不能以与功能规范相同的方式与页面交互,因此您的sign_in实用程序将不适用于它们。

如果您的控制器规格依赖于登录用户,您将需要存根您的current_user方法。如果您使用 Devise,这里有一个来自他们 wiki 的有用页面:https ://github.com/plataformatec/devise/wiki/How-To:-Stub-authentication-in-controller-specs

于 2013-10-09T20:30:01.323 回答