spec/authority/abilities_spec.rb in authority-2.2.0 vs spec/authority/abilities_spec.rb in authority-2.3.0
- old
+ new
@@ -1,126 +1,141 @@
require 'spec_helper'
-require 'support/example_model'
-require 'support/user'
+require 'support/example_classes'
describe Authority::Abilities do
- before :each do
- @user = User.new
- end
+ let(:user) { ExampleUser.new }
+ let(:resource_class) { ExampleResource }
- describe "authorizer" do
+ describe "instance methods" do
- it "should have a class attribute getter for authorizer_name" do
- ExampleModel.should respond_to(:authorizer_name)
- end
+ describe "authorizer_name" do
- it "should have a class attribute setter for authorizer_name" do
- ExampleModel.should respond_to(:authorizer_name=)
- end
+ it "has a class attribute getter for authorizer_name" do
+ expect(resource_class).to respond_to(:authorizer_name)
+ end
- it "should have a default authorizer_name of 'ApplicationAuthorizer'" do
- ExampleModel.authorizer_name.should eq("ApplicationAuthorizer")
- end
+ it "has a class attribute setter for authorizer_name" do
+ expect(resource_class).to respond_to(:authorizer_name=)
+ end
- it "should constantize the authorizer name as the authorizer" do
- ExampleModel.instance_variable_set(:@authorizer, nil)
- ExampleModel.authorizer_name.should_receive(:constantize)
- ExampleModel.authorizer
- end
+ it "has a default authorizer_name of 'ApplicationAuthorizer'" do
+ expect(resource_class.authorizer_name).to eq("ApplicationAuthorizer")
+ end
- it "should memoize the authorizer to avoid reconstantizing" do
- ExampleModel.authorizer
- ExampleModel.authorizer_name.should_not_receive(:constantize)
- ExampleModel.authorizer
end
- it "should raise a friendly error if the authorizer doesn't exist" do
- class NoAuthorizerModel < ExampleModel; end ;
- NoAuthorizerModel.instance_variable_set(:@authorizer, nil)
- NoAuthorizerModel.authorizer_name = 'NonExistentAuthorizer'
- expect { NoAuthorizerModel.authorizer }.to raise_error(Authority::NoAuthorizerError)
+ describe "authorizer" do
+
+ it "constantizes the authorizer name as the authorizer" do
+ resource_class.instance_variable_set(:@authorizer, nil)
+ resource_class.authorizer_name.should_receive(:constantize)
+ resource_class.authorizer
+ end
+
+ it "memoizes the authorizer to avoid reconstantizing" do
+ resource_class.authorizer
+ resource_class.authorizer_name.should_not_receive(:constantize)
+ resource_class.authorizer
+ end
+
+ it "raises a friendly error if the authorizer doesn't exist" do
+ class NoAuthorizerModel < resource_class; end ;
+ NoAuthorizerModel.instance_variable_set(:@authorizer, nil)
+ NoAuthorizerModel.authorizer_name = 'NonExistentAuthorizer'
+ expect { NoAuthorizerModel.authorizer }.to raise_error(Authority::NoAuthorizerError)
+ end
+
end
end
describe "class methods" do
Authority.adjectives.each do |adjective|
method_name = "#{adjective}_by?"
- it "should respond to `#{method_name}`" do
- ExampleModel.should respond_to(method_name)
+ it "responds to `#{method_name}`" do
+ expect(resource_class).to respond_to(method_name)
end
- describe "if given an options hash" do
+ describe "#{method_name}" do
- it "should delegate `#{method_name}` to its authorizer class, passing the options" do
- ExampleModel.authorizer.should_receive(method_name).with(@user, :lacking => 'nothing')
- ExampleModel.send(method_name, @user, :lacking => 'nothing')
+ context "when given an options hash" do
+
+ it "delegates `#{method_name}` to its authorizer class, passing the options" do
+ resource_class.authorizer.should_receive(method_name).with(user, :lacking => 'nothing')
+ resource_class.send(method_name, user, :lacking => 'nothing')
+ end
+
end
- end
+ context "when not given an options hash" do
- describe "if not given an options hash" do
+ it "delegates `#{method_name}` to its authorizer class, passing no options" do
+ resource_class.authorizer.should_receive(method_name).with(user)
+ resource_class.send(method_name, user)
+ end
- it "should delegate `#{method_name}` to its authorizer class, passing no options" do
- ExampleModel.authorizer.should_receive(method_name).with(@user)
- ExampleModel.send(method_name, @user)
end
end
end
end
describe "instance methods" do
+ let(:resource_instance) { resource_class.new }
+
before :each do
- @example_model = ExampleModel.new
- @authorizer = ExampleModel.authorizer.new(@example_model)
+ @authorizer = resource_class.authorizer.new(resource_instance)
end
Authority.adjectives.each do |adjective|
method_name = "#{adjective}_by?"
- it "should respond to `#{method_name}`" do
- @example_model.should respond_to(method_name)
+ it "responds to `#{method_name}`" do
+ expect(resource_instance).to respond_to(method_name)
end
- describe "if given an options hash" do
+ describe "#{method_name}" do
- it "should delegate `#{method_name}` to a new authorizer instance, passing the options" do
- ExampleModel.authorizer.stub(:new).and_return(@authorizer)
- @authorizer.should_receive(method_name).with(@user, :with => 'mayo')
- @example_model.send(method_name, @user, :with => 'mayo')
+ context "when given an options hash" do
+
+ it "delegates `#{method_name}` to a new authorizer instance, passing the options" do
+ resource_class.authorizer.stub(:new).and_return(@authorizer)
+ @authorizer.should_receive(method_name).with(user, :with => 'mayo')
+ resource_instance.send(method_name, user, :with => 'mayo')
+ end
+
end
- end
+ context "when not given an options hash" do
- describe "if not given an options hash" do
-
- it "should delegate `#{method_name}` to a new authorizer instance, passing no options" do
- ExampleModel.authorizer.stub(:new).and_return(@authorizer)
- @authorizer.should_receive(method_name).with(@user)
- @example_model.send(method_name, @user)
+ it "delegates `#{method_name}` to a new authorizer instance, passing no options" do
+ resource_class.authorizer.stub(:new).and_return(@authorizer)
+ @authorizer.should_receive(method_name).with(user)
+ resource_instance.send(method_name, user)
+ end
+
end
end
end
- it "should provide an accessor for its authorizer" do
- @example_model.should respond_to(:authorizer)
+ it "provides an accessor for its authorizer" do
+ expect(resource_instance).to respond_to(:authorizer)
end
# When checking instance methods, we want to ensure that every check uses a new
# instance of the authorizer. Otherwise, you might check, make a change to the
# model instance, check again, and get an outdated answer.
- it "should always create a new authorizer instance when accessing the authorizer" do
- @example_model.class.authorizer.should_receive(:new).with(@example_model).twice
- 2.times { @example_model.authorizer }
+ it "always creates a new authorizer instance when accessing the authorizer" do
+ resource_instance.class.authorizer.should_receive(:new).with(resource_instance).twice
+ 2.times { resource_instance.authorizer }
end
end
end