Permalink
Browse files

More spec cleanup

  • Loading branch information...
1 parent 9f5ecd4 commit e65b12e28be77c0ca644961411338d9ac3643a61 @nathanl committed Dec 8, 2012
Showing with 79 additions and 68 deletions.
  1. +48 −41 spec/authority/abilities_spec.rb
  2. +31 −27 spec/authority/integration_spec.rb
@@ -4,41 +4,47 @@
describe Authority::Abilities do
- before :each do
- @user = User.new
- end
+ let(:user) { User.new }
- describe "authorizer" do
+ describe "instance methods" do
- it "has a class attribute getter for authorizer_name" do
- expect(ExampleModel).to respond_to(:authorizer_name)
- end
+ describe "authorizer_name" do
- it "has a class attribute setter for authorizer_name" do
- expect(ExampleModel).to respond_to(:authorizer_name=)
- end
+ it "has a class attribute getter for authorizer_name" do
+ expect(ExampleModel).to respond_to(:authorizer_name)
+ end
- it "has a default authorizer_name of 'ApplicationAuthorizer'" do
- expect(ExampleModel.authorizer_name).to eq("ApplicationAuthorizer")
- end
+ it "has a class attribute setter for authorizer_name" do
+ expect(ExampleModel).to respond_to(:authorizer_name=)
+ end
- it "constantizes 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(ExampleModel.authorizer_name).to eq("ApplicationAuthorizer")
+ end
- it "memoizes the authorizer to avoid reconstantizing" do
- ExampleModel.authorizer
- ExampleModel.authorizer_name.should_not_receive(:constantize)
- ExampleModel.authorizer
end
- it "raises 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
+ ExampleModel.instance_variable_set(:@authorizer, nil)
+ ExampleModel.authorizer_name.should_receive(:constantize)
+ ExampleModel.authorizer
+ end
+
+ it "memoizes the authorizer to avoid reconstantizing" do
+ ExampleModel.authorizer
+ ExampleModel.authorizer_name.should_not_receive(:constantize)
+ ExampleModel.authorizer
+ end
+
+ it "raises 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)
+ end
+
end
end
@@ -57,17 +63,17 @@ class NoAuthorizerModel < ExampleModel; end ;
context "when given an options hash" do
it "delegates `#{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')
+ ExampleModel.authorizer.should_receive(method_name).with(user, :lacking => 'nothing')
+ ExampleModel.send(method_name, user, :lacking => 'nothing')
end
end
context "when not given an options hash" do
it "delegates `#{method_name}` to its authorizer class, passing no options" do
- ExampleModel.authorizer.should_receive(method_name).with(@user)
- ExampleModel.send(method_name, @user)
+ ExampleModel.authorizer.should_receive(method_name).with(user)
+ ExampleModel.send(method_name, user)
end
end
@@ -80,16 +86,17 @@ class NoAuthorizerModel < ExampleModel; end ;
describe "instance methods" do
+ let(:example_model) { ExampleModel.new }
+
before :each do
- @example_model = ExampleModel.new
- @authorizer = ExampleModel.authorizer.new(@example_model)
+ @authorizer = ExampleModel.authorizer.new(example_model)
end
Authority.adjectives.each do |adjective|
method_name = "#{adjective}_by?"
it "responds to `#{method_name}`" do
- expect(@example_model).to respond_to(method_name)
+ expect(example_model).to respond_to(method_name)
end
describe "#{method_name}" do
@@ -98,8 +105,8 @@ class NoAuthorizerModel < ExampleModel; end ;
it "delegates `#{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')
+ @authorizer.should_receive(method_name).with(user, :with => 'mayo')
+ example_model.send(method_name, user, :with => 'mayo')
end
end
@@ -108,8 +115,8 @@ class NoAuthorizerModel < ExampleModel; end ;
it "delegates `#{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)
+ @authorizer.should_receive(method_name).with(user)
+ example_model.send(method_name, user)
end
end
@@ -119,15 +126,15 @@ class NoAuthorizerModel < ExampleModel; end ;
end
it "provides an accessor for its authorizer" do
- expect(@example_model).to respond_to(:authorizer)
+ expect(example_model).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 "always creates 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 }
+ example_model.class.authorizer.should_receive(:new).with(example_model).twice
+ 2.times { example_model.authorizer }
end
end
@@ -4,10 +4,8 @@
describe "integration from user through model to authorizer" do
- before :each do
- @user = User.new
- @example_model = ExampleModel.new
- end
+ let(:user) { User.new }
+ let(:model_instance) { ExampleModel.new }
describe "class methods" do
@@ -16,20 +14,24 @@
adjective = Authority.abilities[verb]
adjective_method = "#{adjective}_by?"
- describe "if given an options hash" do
+ describe "#{adjective_method}" do
+
+ describe "if given an options hash" do
+
+ it "delegates `#{adjective_method}` to its authorizer class, passing the options" do
+ ExampleModel.authorizer.should_receive(adjective_method).with(user, :lacking => 'nothing')
+ user.send(verb_method, ExampleModel, :lacking => 'nothing')
+ end
- it "delegates `#{adjective_method}` to its authorizer class, passing the options" do
- ExampleModel.authorizer.should_receive(adjective_method).with(@user, :lacking => 'nothing')
- @user.send(verb_method, ExampleModel, :lacking => 'nothing')
end
- end
+ describe "if not given an options hash" do
- describe "if not given an options hash" do
+ it "delegates `#{adjective_method}` to its authorizer class, passing no options" do
+ ExampleModel.authorizer.should_receive(adjective_method).with(user)
+ user.send(verb_method, model_instance)
+ end
- it "delegates `#{adjective_method}` to its authorizer class, passing no options" do
- ExampleModel.authorizer.should_receive(adjective_method).with(@user)
- @user.send(verb_method, @example_model)
end
end
@@ -41,31 +43,33 @@
describe "instance methods" do
before :each do
- @user = User.new
- @example_model = ExampleModel.new
- @authorizer = ExampleModel.authorizer.new(@example_model)
- ExampleModel.authorizer.stub(:new).and_return(@authorizer)
+ @authorizer_instance = ExampleModel.authorizer.new(model_instance)
+ ExampleModel.authorizer.stub(:new).and_return(@authorizer_instance)
end
Authority.verbs.each do |verb|
verb_method = "can_#{verb}?"
adjective = Authority.abilities[verb]
adjective_method = "#{adjective}_by?"
- describe "if given an options hash" do
+ describe "#{adjective_method}" do
+
+ describe "if given an options hash" do
+
+ it "delegates `#{adjective_method}` to a new authorizer instance, passing the options" do
+ @authorizer_instance.should_receive(adjective_method).with(user, :consistency => 'mushy')
+ user.send(verb_method, model_instance, :consistency => 'mushy')
+ end
- it "delegates `#{adjective_method}` to a new authorizer instance, passing the options" do
- @authorizer.should_receive(adjective_method).with(@user, :consistency => 'mushy')
- @user.send(verb_method, @example_model, :consistency => 'mushy')
end
- end
+ describe "if not given an options hash" do
+
+ it "delegates `#{adjective_method}` to a new authorizer instance, passing no options" do
+ @authorizer_instance.should_receive(adjective_method).with(user)
+ user.send(verb_method, model_instance)
+ end
- describe "if not given an options hash" do
-
- it "delegates `#{adjective_method}` to a new authorizer instance, passing no options" do
- @authorizer.should_receive(adjective_method).with(@user)
- @user.send(verb_method, @example_model)
end
end

0 comments on commit e65b12e

Please sign in to comment.