rail3/rspec/devise:rspec 控制器测试失败,除非我添加一个 dummy=subject.current_user.inspect [英] rail3/rspec/devise: rspec controller test fails unless I add a dummy=subject.current_user.inspect

查看:11
本文介绍了rail3/rspec/devise:rspec 控制器测试失败,除非我添加一个 dummy=subject.current_user.inspect的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让 RSpec 为一个简单的脚手架应用程序工作,从 rspec 脚手架测试开始.

I'm trying to get RSpec working for a simple scaffolded app, starting with the rspec scaffold tests.

根据设计 wiki,我添加了各种设计配置条目、用户和管理员的工厂,并且我在规范控制器中做的第一件事是 login_admin.

Per the devise wiki, I have added the various devise config entries, a factory for a user and an admin, and the first things I do in my spec controller is login_admin.

最奇怪的事情是……我所有的规范都失败了,除非我在 it ... do 行之后添加以下语句:

Weirdest thing, though... all my specs fail UNLESS I add the following statement right after the it ... do line:

dummy=subject.current_user.inspect

(使用该行,如下所示,规范通过.没有该行,所有测试都失败,分配为 nil 而不是预期值.我只是碰巧发现,当我放置一些 puts 语句以查看是否current_user 设置正确.)

(With the line, as shown below, the specs pass. Without that line, all tests fail with the assigns being nil instead of the expected value. I only happened to discover that when I was putting some puts statements to see if the current_user was being set correctly.)

所以它的行为就像是那个虚拟语句以某种方式强制"加载、刷新或识别 current_user.

So what it acts like is that dummy statement somehow 'forces' the current_user to be loaded or refreshed or recognized.

谁能解释一下发生了什么,我应该做些什么不同的事情,这样我就不需要虚拟语句?

Can anyone explain what's going on, and what I should be doing differently so I don't need the dummy statement?

#specs/controllers/brokers_controller_spec.rb
describe BrokersController do
  login_admin

  def valid_attributes
    {:name => "Bill", :email => "rspec_broker@example.com", :company => "Example Inc", :community_id => 1}
  end

  def valid_session
    {}
  end

  describe "GET index" do
    it "assigns all brokers as @brokers" do
      dummy=subject.current_user.inspect    # ALL SPECS FAIL WITHOUT THIS LINE!
      broker = Broker.create! valid_attributes
      get :index, {}, valid_session
      assigns(:brokers).should eq([broker])
    end
  end
  describe "GET show" do
    it "assigns the requested broker as @broker" do
      dummy=subject.current_user.inspect  # ALL SPECS FAIL WITHOUT THIS LINE!
      broker = Broker.create! valid_attributes
      get :show, {:id => broker.to_param}, valid_session
      assigns(:broker).should eq(broker)
    end
  end

根据设计 wiki,这里是我登录 :user 或 :admin 的方式

and per the devise wiki here is how I login a :user or :admin

#spec/support/controller_macros.rb
module ControllerMacros
  def login_admin
    before(:each) do
      @request.env["devise.mapping"] = Devise.mappings[:admin]
      sign_in Factory.create(:admin) # Using factory girl as an example
    end
  end

  def login_user
    before(:each) do
      @request.env["devise.mapping"] = Devise.mappings[:user]
      user = Factory.create(:user)
      user.confirm! # or set a confirmed_at inside the factory. Only necessary if you are using the confirmable module
      sign_in user
    end
  end
end

推荐答案

好难受!谢谢罗宾,我已经在谷歌上搜索了几个小时,终于看到了你的帖子;现在我的控制器测试正在运行:)

What a struggle! Thank you Robin, I've been googling on this for hours and finally saw your post; now my controller tests are working :)

为了补充您的答案,我想出了如何将设计会话放入 valid_session 哈希中,这允许控制器测试按照 rails 生成的方式正常运行.

To add to your answer, I figured out how to get the devise session into the valid_session hash, which allows the controller tests to run properly as generated by rails.

def valid_session
  {"warden.user.user.key" => session["warden.user.user.key"]}
end

这篇关于rail3/rspec/devise:rspec 控制器测试失败,除非我添加一个 dummy=subject.current_user.inspect的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆