Ruby on rails RSpec控制器规范中的CanCan

Ruby on rails RSpec控制器规范中的CanCan,ruby-on-rails,rspec,devise,cancan,Ruby On Rails,Rspec,Devise,Cancan,我花了一天的大部分时间试图解决一个控制器规范的问题,而当前的解决方法似乎对我来说是不可接受的。有人知道这是为什么吗。。。以及我应该做些什么 给定如下的简单层次结构,以及以下ability.rb,properties_controller_spec.rb不允许下面的spec在没有行说明的情况下通过: ability = Ability.new(subject.current_user) 你能告诉我为什么会这样吗 谢谢 型号: class Account < ActiveRecord::Ba

我花了一天的大部分时间试图解决一个控制器规范的问题,而当前的解决方法似乎对我来说是不可接受的。有人知道这是为什么吗。。。以及我应该做些什么

给定如下的简单层次结构,以及以下ability.rb,properties_controller_spec.rb不允许下面的spec在没有行说明的情况下通过:

ability = Ability.new(subject.current_user)
你能告诉我为什么会这样吗

谢谢

型号:

class Account < ActiveRecord::Base
  has_many :properties, :dependent => :nullify
end

class Property < ActiveRecord::Base
  belongs_to :account
end

class User < Refinery::Core::BaseModel #for RefineryCMS integration
  belongs_to :account
end
属性\u控制器\u规范rb:

require 'spec_helper'

describe PropertiesController do
  def valid_attributes
  describe "Authenticated as Property user" do
    describe "PUT update" do
      describe "with invalid params" do
        it "re-renders the 'edit' template" do
          property = FactoryGirl.create(:property, account: property_user.account)
          # Trigger the behavior that occurs when invalid params are submitted
          Property.any_instance.stub(:save).and_return(false)
          ability = Ability.new(subject.current_user) # seriously?
          put :update, {:id => property.to_param, :property => {  }}, {}
          response.should render_template("edit")
        end
      end
    end
  end
end
阿格!我自己找到的

这是:

config.include designe::TestHelpers,:type=>:controller

以下是按照设计文件的指示,在属性用户中签名的代码。(讨论中的局部变量是在包含的全局_variables.rb中创建的。这些变量到处都在使用。)


您是否已将rspec配置为使用Desive helpers?config.include designe::TestHelpers,:type=>:controller您是否已从测试中登录任何用户?否则该功能将使用新的用户对象。。。(您可以使用上面评论中提到的帮助器登录用户。)我确实配置了designe::TestHelpers。我的台词如下:Arg!这里是:config.include designe::TestHelpers,:type=>:controller下面是按照designe文档的指示登录属性用户的代码。(讨论中的局部变量是在包含的全局变量.rb中创建的。这些变量在所有地方都使用。)def signed_作为属性用户属性用户用户添加角色“用户”sign_属性用户端def sign_作为属性用户属性用户添加角色“用户”post_通过重定向用户会话路径“用户”[电子邮件]=>property\u user.email,'user[密码]=>property\u user.password结束
require 'spec_helper'

describe PropertiesController do
  def valid_attributes
  describe "Authenticated as Property user" do
    describe "PUT update" do
      describe "with invalid params" do
        it "re-renders the 'edit' template" do
          property = FactoryGirl.create(:property, account: property_user.account)
          # Trigger the behavior that occurs when invalid params are submitted
          Property.any_instance.stub(:save).and_return(false)
          ability = Ability.new(subject.current_user) # seriously?
          put :update, {:id => property.to_param, :property => {  }}, {}
          response.should render_template("edit")
        end
      end
    end
  end
end
def signed_in_as_a_property_user
  property_user.add_role "User" 
  sign_in property_user 
end

def sign_in_as_a_property_user 
  property_user.add_role 'User' 
  post_via_redirect user_session_path, 
    'user[email]' => property_user.email,
    'user[password]' => property_user.password
end