运行测试时Rails控制器执行具有相同名称的Rspec Helper方法

时间:2013-06-19 20:06:41

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

我的ApplicationController中有以下代码:

application_controller.rb

class ApplicationController < ActionController::Base
 protect_from_forgery
 include SessionsHelper  
end

应用程序/助手/ sessions_helper.rb

module SessionsHelper
 def sign_in(user)
  cookies.permanent[:remember_token]= user.remember_token
  self.current_user = user 
 end
end 

我正在使用rspec-rails并在我的utilities.rb文件中使用了一个帮助方法'sign_in': 规格/支持/ utilities.rb

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

我的static_pages_spec看起来像这样:

 describe "When signed in" do
  let(:user) {FactoryGirl.create(:user)}
  before do
    sign_in user
  end
  describe "When no lists are present" do

    it "should show message asking to create list" do
      page.should have_selector("div",text:"You have no lists ! Create a list now !")
    end
  end
 end

当我运行测试时,它会给我这个错误:

 StaticPages Home page When signed in When no lists are present should show message asking to create list
     Failure/Error: sign_in user
     NoMethodError:
       undefined method visit' for #<SessionsController:0x00000003630c10>
     # ./spec/support/utilities.rb:2:insign_in'
     # ./app/controllers/sessions_controller.rb:13:in create'
     # (eval):2:inclick_button'
     # ./spec/support/utilities.rb:5:in sign_in'
     # ./spec/requests/static_pages_spec.rb:24:inblock (4 levels) in top (required)>'

这是SessionsController的“创建”动作:

def create
 user = User.find_by_email(params[:session][:email])
 if user && user.authenticate(params[:session][:password])
   sign_in user  # CODE ERRORS OUT HERE..sign_in of spec/support/utilities.rb executed
   redirect_back_or
 else
   flash.now[:error]= "Invalid email/password"
   render 'new' ,layout: "signin_fail"
 end
end

TL,DR :所以,我的SessionsController正在执行错误的“sign_in”方法(属于specs / support而不是app / helpers.session_helper.rb的方法)。当我将spec / support中的帮助程序名称更改为“log_in”时,问题就解决了。这可能是什么问题?

0 个答案:

没有答案