Note that there are some explanatory texts on larger screens.

plurals
  1. POCanCan in RSpec Controller spec
    primarykey
    data
    text
    <p>I spent most of the day trying to root out a problem with a controller spec, and the current workaround seems unacceptable to me. Any take on why this works? ... and what I should do instead.</p> <p>Given a simple hierarchy as follows, and the following ability.rb, the properties_controller_spec.rb does not allow the spec below to pass without the line saying:</p> <pre><code>ability = Ability.new(subject.current_user) </code></pre> <p>Can you tell me why this would be?</p> <p>Thanks!</p> <p>Models:</p> <pre><code>class Account &lt; ActiveRecord::Base has_many :properties, :dependent =&gt; :nullify end class Property &lt; ActiveRecord::Base belongs_to :account end class User &lt; Refinery::Core::BaseModel #for RefineryCMS integration belongs_to :account end </code></pre> <p>Ability.rb:</p> <pre><code>class Ability include CanCan::Ability def initialize(user) user ||= User.new if user.has_role? :user can [:read, :create, :update, :destroy], Property, account_id: user.account_id else can [:show], Property end end end </code></pre> <p>properties_contoller_spec.rb:</p> <pre><code>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 =&gt; property.to_param, :property =&gt; { }}, {} response.should render_template("edit") end end end end end </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload