在请求规范中对验证进行验证

时间:2011-04-26 07:46:24

标签: ruby-on-rails-3 rspec integration-testing stubbing

编写请求规范时,如何设置会话和/或存根控制器方法? 我正在尝试在我的集成测试中删除身份验证 - rspec / requests

以下是测试的示例

require File.dirname(__FILE__) + '/../spec_helper'
require File.dirname(__FILE__) + '/authentication_helpers'


describe "Messages" do
  include AuthenticationHelpers

  describe "GET admin/messages" do
    before(:each) do
      @current_user = Factory :super_admin
      login(@current_user)
    end

    it "displays received messages" do
      sender = Factory :jonas
      direct_message = Message.new(:sender_id => sender.id, :subject => "Message system.", :content => "content", :receiver_ids => [@current_user.id])
      direct_message.save
      get admin_messages_path
      response.body.should include(direct_message.subject) 
    end
  end
end

帮助者:

module AuthenticationHelpers
  def login(user)
    session[:user_id] = user.id # session is nil
    #controller.stub!(:current_user).and_return(user) # controller is nil
  end
end

处理身份验证的ApplicationController:

class ApplicationController < ActionController::Base
  protect_from_forgery

  helper_method :current_user
  helper_method :logged_in?

  protected

  def current_user  
    @current_user ||= User.find(session[:user_id]) if session[:user_id]  
  end

  def logged_in?
    !current_user.nil?
  end
end

为什么无法访问这些资源?

1) Messages GET admin/messages displays received messages
     Failure/Error: login(@current_user)
     NoMethodError:
       undefined method `session' for nil:NilClass
     # ./spec/requests/authentication_helpers.rb:3:in `login'
     # ./spec/requests/message_spec.rb:15:in `block (3 levels) in <top (required)>'

5 个答案:

答案 0 :(得分:100)

请求规范是ActionDispatch::IntegrationTest周围的瘦包装,它不像控制器规范(包裹ActionController::TestCase)那样工作。即使有可用的会话方法,我也不认为它是受支持的(即它可能存在,因为包含在其他实用程序中的模块也包含该方法)。

我建议您通过发布用于验证用户身份的操作来登录。如果您为所有用户工厂设置密码'password'(例如),那么您可以执行以下操作:

def login(user)
  post login_path, :login => user.login, :password => 'password'
end

答案 1 :(得分:59)

对于设计用户的注意事项......

顺便说一下,如果你使用Devise,@ David Chelimsky的答案可能需要稍微调整一下。我在集成/请求测试中所做的事情(感谢this StackOverflow post):

# file: spec/requests_helper.rb
def login(user)
  post_via_redirect user_session_path, 'user[email]' => user.email, 'user[password]' => user.password
end

答案 2 :(得分:3)

FWIW,在将我的Test :: Unit测试移植到RSpec时,我希望能够在我的请求规范中使用多个(设计)会话进行登录。这需要一些挖掘,但这对我有用。使用Rails 3.2.13和RSpec 2.13.0。

# file: spec/support/devise.rb
module RequestHelpers
  def login(user)
    ActionController::IntegrationTest.new(self).open_session do |sess|
      u = users(user)

      sess.post '/users/sign_in', {
        user: {
          email: u.email,
          password: 'password'
        }
      }

      sess.flash[:alert].should be_nil
      sess.flash[:notice].should == 'Signed in successfully.'
      sess.response.code.should == '302'
    end
  end
end

include RequestHelpers

和...

# spec/request/user_flows.rb
require 'spec_helper'

describe 'User flows' do
  fixtures :users

  it 'lets a user do stuff to another user' do
    karl = login :karl
    karl.get '/users'
    karl.response.code.should eq '200'

    karl.xhr :put, "/users/#{users(:bob).id}", id: users(:bob).id,
      "#{users(:bob).id}-is-funny" => 'true'

    karl.response.code.should eq '200'
    User.find(users(:bob).id).should be_funny

    bob = login :bob
    expect { bob.get '/users' }.to_not raise_exception

    bob.response.code.should eq '200'
  end
end

修改:修复错误

答案 3 :(得分:-1)

您也可以很容易地将会话存根。

controller.session.stub(:[]).with(:user_id).and_return(<whatever user ID>)

所有ruby特殊运算符确实都是方法。致电1+11.+(1)相同,这意味着+只是一种方法。同样,session[:user_id][]上的调用方法session相同,为session.[](:user_id)

答案 4 :(得分:-3)