私は一日のほとんどをコントローラーの仕様の問題を根絶するために費やしましたが、現在の回避策は受け入れられないようです。これが機能する理由について何か考えはありますか?...そして私が代わりにすべきこと。
次のような単純な階層と次のability.rbが与えられた場合、properties_controller_spec.rbは、次の行がない場合に以下の仕様を渡すことを許可しません。
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
Ability.rb:
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
property_contoller_spec.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