SORU
26 NİSAN 2011, Salı


İstek Stubbing kimlik doğrulama spec

Bir istek spec yazmaya ne zaman, nasıl ve/veya saplama denetleyicisi yöntemleri seans için? Dışarı entegrasyon benim testlerde kimlik doğrulama saplama - /istekleri rspec için çalışıyorum

İşte bir test örneği

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

Yardımcı:

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

Ve Kimlik Doğrulama işleyen 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

Neden bu kaynaklara erişmek mümkün değil mi?

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

CEVAP
27 NİSAN 2011, ÇARŞAMBA


İstek spec denetleyicisi özellikleri ActionController::TestCase wrap) gibi çalışmıyor ActionDispatch::IntegrationTest, etrafında ince bir sarıcı. Oturum bir yöntem var olsa bile, (yani diğer kamu hizmetleri için yer aldığı bir modül de bu yöntemi içerir çünkü muhtemelen var) desteklenen olduğunu sanmıyorum.

Kullanıcı kimlik doğrulaması için kullandığınız ne olursa olsun yayınlayarak günlüğü tavsiye ederim. Eğer 'şifre' (örneğin) için Kullanıcı fabrikalar, o zaman şöyle bir şey yapabilirsin: . yaparsanız

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

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • AndyMcMillinTV

    AndyMcMillin

    6 HAZİRAN 2007
  • Boiler Room

    Boiler Room

    10 Mayıs 2012
  • RaquelGamesBR

    RaquelGamesB

    20 HAZİRAN 2009