ruby-on-rails - 使用 Rspec、NiftyAuthentication 进行测试

标签 ruby-on-rails rspec rspec-rails

我正在使用 Ryan Bates 的 nifty:authentication,并开始使用 Rspec 进行测试。与这个问题斗争了几个星期,但仍然不明白发生了什么。

我的 Controller 只是调用

before_filter :login_required, :except => [:login]

在 lib/controller_authentication 中定义

def self.included(controller)
    controller.send :helper_method, :current_account, :logged_in?, :redirect_to_target_or_default
  end

  def current_account
    @current_account ||= Account.find(session[:account_id]) if session[:account_id]
  end

  def logged_in?
    current_account
  end

  def login_required
    unless logged_in?
      store_target_location
      redirect_to login_url, :alert => "You must first answer me these riddles three log in or sign up before accessing this page."
    end
  end

  def redirect_to_target_or_default(default, *args)
    redirect_to(session[:return_to] || default, *args)
    session[:return_to] = nil
  end

  private

  def store_target_location
    session[:return_to] = request.url
  end
end

应用程序按预期运行,但测试每次都失败。无论我尝试什么,我都会收到redirect_to login_url, :alert =>“您必须首先...登录”页面。

我尝试过的事情:

controller.stub!( :login_required )
ControllerAuthentication.stub!(:current_account).and_return(Account.where(:username => 'ej0c').first)
#ControllerAuthentication.stub!(:logged_in?).and_return(Account.where(:username => 'ej0c').first)
ControllerAuthentication.stub!(:login_required).and_return(true)
MyDigisController.stub!( :login_required ).and_return(true)

我认为这意味着我错过了这件事的整个理论。我怎样才能让我的登录正常工作?


我尝试按照 Punit 的建议进行操作: [上一页]

require 'spec_helper'

describe "View event details" do

  it "Should show a table of events" do
     @account = Account.where(:username => 'ej0c').first
     puts @account.inspect
     controller.stub!(:current_account).and_return(@account)
     controller.stub!(:logged_in?).and_return(true)
     session[:account_id] = @account.id
      visit '/my_digis/66/custom_events'
      page.should have_content('Events')

  end
end

@account.inspect 显示得很好,但我也得到了

An expectation of :current_account was set on nil. Called from C:/Users/Ed/webapps/whendidji3/spec/con
.rb:8:in `block (2 levels) in <top (required)>'. Use allow_message_expectations_on_nil to disable warn
An expectation of :logged_in? was set on nil. Called from C:/Users/Ed/webapps/whendidji3/spec/controll
:in `block (2 levels) in <top (required)>'. Use allow_message_expectations_on_nil to disable warnings.

感谢您的详细解释,因为我已经从高到低进行了搜索以了解发生了什么。

最佳答案

您使用的是普通规范而不是 Controller 规范,这意味着未设置变量“ Controller ”。

要使用 Controller 规范,您需要将 Controller 类名称传递到描述 block 而不是字符串。

describe MyController do

参见http://rspec.info/rails/writing/controllers.html

一旦你这样做了,你应该能够使用你最初的想法来 stub login_required

关于ruby-on-rails - 使用 Rspec、NiftyAuthentication 进行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7028167/

相关文章:

ruby-on-rails - ActionMailer::Base.deliveries 在 Rspec 中始终为空

ruby-on-rails - rails : How to post a file via HTTP as multipart/form-data to Facebook?

ruby-on-rails - 页面刷新后在 Ruby on Rails 中获取 Request Referer

ruby-on-rails - FactoryGirl 没有给出有用的验证错误

ruby-on-rails - Rspec - Controller 测试错误 - Paperclip::AdapterRegistry::NoHandlerError: 找不到 "#<File:0x531beb0>"的处理程序

ruby-on-rails - Controller 规范未知关键字 : id

ruby-on-rails - 在 Rails 应用程序中上传视频

ruby-on-rails - 将 I18n 与 Rails 一起使用时的重定向将正斜杠编码为 %2F

ruby-on-rails - 使用 Rspec + Capybara 在 Rails 中测试错误页面

ruby-on-rails - RSpec 失败(未定义方法 `id' 为 nil :NilClass ) with has_many through model