Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Banish the explicit use of subject in specs

  • Loading branch information...
commit e752a0855ce9b39d6a2ca039548c4adc01800bb8 1 parent 39c4bc7
@cgriego authored
Showing with 425 additions and 413 deletions.
  1. +1 −1  active_attr.gemspec
  2. +12 −12 spec/functional/active_attr/attribute_defaults_spec.rb
  3. +12 −12 spec/functional/active_attr/attributes_spec.rb
  4. +3 −3 spec/functional/active_attr/chainable_initialization_spec.rb
  5. +0 −2  spec/functional/active_attr/mass_assignment_security_spec.rb
  6. +66 −66 spec/functional/active_attr/matchers/have_attribute_matcher_spec.rb
  7. +22 −19 spec/functional/active_attr/model_spec.rb
  8. +5 −5 spec/functional/active_attr/serialization_spec.rb
  9. +45 −45 spec/functional/active_attr/typecasted_attributes_spec.rb
  10. +12 −12 spec/support/mass_assignment_shared_examples.rb
  11. +6 −6 spec/unit/active_attr/attribute_defaults_spec.rb
  12. +6 −6 spec/unit/active_attr/attribute_definition_spec.rb
  13. +38 −38 spec/unit/active_attr/attributes_spec.rb
  14. +16 −16 spec/unit/active_attr/logger_spec.rb
  15. +0 −2  spec/unit/active_attr/mass_assignment_security_spec.rb
  16. +1 −1  spec/unit/active_attr/mass_assignment_spec.rb
  17. +3 −4 spec/unit/active_attr/matchers_spec.rb
  18. +75 −75 spec/unit/active_attr/query_attributes_spec.rb
  19. +10 −10 spec/unit/active_attr/typecasted_attributes_spec.rb
  20. +9 −7 spec/unit/active_attr/typecasting/big_decimal_typecaster_spec.rb
  21. +27 −25 spec/unit/active_attr/typecasting/boolean_typecaster_spec.rb
  22. +12 −10 spec/unit/active_attr/typecasting/date_time_typecaster_spec.rb
  23. +14 −12 spec/unit/active_attr/typecasting/date_typecaster_spec.rb
  24. +6 −4 spec/unit/active_attr/typecasting/float_typecaster_spec.rb
  25. +8 −6 spec/unit/active_attr/typecasting/integer_typecaster_spec.rb
  26. +3 −1 spec/unit/active_attr/typecasting/object_typecaster_spec.rb
  27. +5 −3 spec/unit/active_attr/typecasting/string_typecaster_spec.rb
  28. +3 −5 spec/unit/active_attr/typecasting_spec.rb
  29. +5 −5 spec/unit/active_attr/version_spec.rb
View
2  active_attr.gemspec
@@ -21,6 +21,6 @@ Gem::Specification.new do |gem|
gem.add_development_dependency "bundler", "~> 1.0"
gem.add_development_dependency "factory_girl", ">= 2.2", "< 4.0"
gem.add_development_dependency "rake", "~> 0.9.0"
- gem.add_development_dependency "rspec", "~> 2.6"
+ gem.add_development_dependency "rspec", "~> 2.11"
gem.add_development_dependency "tzinfo", "~> 0.3.29"
end
View
24 spec/functional/active_attr/attribute_defaults_spec.rb
@@ -6,7 +6,7 @@
module ActiveAttr
describe AttributeDefaults do
- subject { model_class.new }
+ subject(:model) { model_class.new }
let :model_class do
Class.new.tap do |model_class|
@@ -20,20 +20,20 @@ module ActiveAttr
before { model_class.attribute :first_name, :default => "John" }
it "the attribute getter returns the string by default" do
- subject.first_name.should == "John"
+ model.first_name.should == "John"
end
it "#attributes includes the default attributes" do
- subject.attributes["first_name"].should == "John"
+ model.attributes["first_name"].should == "John"
end
it "#read_attribute returns the string by default" do
- subject.read_attribute("first_name").should == "John"
+ model.read_attribute("first_name").should == "John"
end
it "assigning nil sets nil as the attribute value" do
- subject.first_name = nil
- subject.first_name.should be_nil
+ model.first_name = nil
+ model.first_name.should be_nil
end
it "mutating the default value does not mutate the attribute definition" do
@@ -46,7 +46,7 @@ module ActiveAttr
before { model_class.attribute :admin, :default => false }
it "the attribute getter returns false by default" do
- subject.admin.should == false
+ model.admin.should == false
end
end
@@ -54,7 +54,7 @@ module ActiveAttr
before { model_class.attribute :remember_me, :default => true }
it "the attribute getter returns true by default" do
- subject.remember_me.should == true
+ model.remember_me.should == true
end
end
@@ -62,7 +62,7 @@ module ActiveAttr
before { model_class.attribute :roles, :default => [] }
it "the attribute getter returns an empty array by default" do
- subject.roles.should == []
+ model.roles.should == []
end
end
@@ -70,11 +70,11 @@ module ActiveAttr
before { model_class.attribute :created_at, :default => lambda { Time.now } }
it "the attribute getter returns a Time instance" do
- subject.created_at.should be_a_kind_of Time
+ model.created_at.should be_a_kind_of Time
end
it "the attribute default is only evaulated once per instance" do
- subject.created_at.should == subject.created_at
+ model.created_at.should == model.created_at
end
it "the attribute default is different per instance" do
@@ -86,7 +86,7 @@ module ActiveAttr
before { model_class.attribute :id, :default => lambda { object_id } }
it "the attribute getter returns the default based on the instance" do
- subject.id.should == subject.object_id
+ model.id.should == model.object_id
end
end
View
24 spec/functional/active_attr/attributes_spec.rb
@@ -23,10 +23,10 @@ def id
end
end
- subject { model_class.new }
+ subject(:model) { model_class.new }
it "correctly defines methods for the attributes instead of relying on method_missing" do
- subject.id.should be_nil
+ model.id.should be_nil
end
end
@@ -95,7 +95,7 @@ def id
context "serializing a model" do
let(:first_name) { "Chris" }
- let :instance do
+ let :model do
model_class.new.tap do |model|
model.first_name = first_name
end
@@ -129,34 +129,34 @@ def self.name
end
it "includes unassigned, defined attributes" do
- subject.keys.should include("last_name")
- subject["last_name"].should be_nil
+ serialized_model.keys.should include("last_name")
+ serialized_model["last_name"].should be_nil
end
end
describe "#as_json" do
- subject { instance.as_json["person"] }
+ subject(:serialized_model) { model.as_json["person"] }
include_examples "serialization method"
end
describe "#serializable_hash" do
- subject { instance.serializable_hash }
+ subject(:serialized_model) { model.serializable_hash }
include_examples "serialization method"
end
describe "#to_json" do
- subject { ActiveSupport::JSON.decode(instance.to_json)["person"] }
+ subject(:serialized_model) { ActiveSupport::JSON.decode(model.to_json)["person"] }
include_examples "serialization method"
end
describe "#to_xml" do
- subject { Hash.from_xml(instance.to_xml)["person"] }
+ subject(:serialized_model) { Hash.from_xml(model.to_xml)["person"] }
include_examples "serialization method"
end
end
context "building with FactoryGirl" do
- subject { FactoryGirl.build(:person) }
+ subject(:model) { FactoryGirl.build(:person) }
before do
Object.const_set("Person", model_class)
@@ -187,8 +187,8 @@ def self.name
end
it "sets the attributes" do
- subject.first_name.should eq "Chris"
- subject.last_name.should == "Griego"
+ model.first_name.should eq "Chris"
+ model.last_name.should == "Griego"
end
end
View
6 spec/functional/active_attr/chainable_initialization_spec.rb
@@ -4,12 +4,12 @@
module ActiveAttr
describe ChainableInitialization do
- subject { model_class.new("arg") }
+ subject(:model) { model_class.new("arg") }
shared_examples "chained initialization" do
describe "#initialize" do
- it { expect { subject }.not_to raise_error }
- it { subject.initialized?.should be_true }
+ it { expect { model }.not_to raise_error }
+ it { model.initialized?.should be_true }
end
end
View
2  spec/functional/active_attr/mass_assignment_security_spec.rb
@@ -4,8 +4,6 @@
module ActiveAttr
describe MassAssignmentSecurity, :mass_assignment do
context "integrating with strong_parameters", :active_model_version => "~> 3.2.0" do
- subject { model_class }
-
before do
require "strong_parameters"
View
132 spec/functional/active_attr/matchers/have_attribute_matcher_spec.rb
@@ -30,13 +30,13 @@ def self.name
end
describe "#matches?" do
- it { subject.matches?(model_class).should be_false }
+ it { matcher.matches?(model_class).should be_false }
end
describe "#failure_message" do
- before { subject.matches?(model_class) }
+ before { matcher.matches?(model_class) }
- it { subject.failure_message.should == %{expected #{model_class.name} to include ActiveAttr::Attributes} }
+ it { matcher.failure_message.should == %{expected #{model_class.name} to include ActiveAttr::Attributes} }
end
end
@@ -52,13 +52,13 @@ def self.name
end
describe "#matches?" do
- it { subject.matches?(model_class).should be_false }
+ it { matcher.matches?(model_class).should be_false }
end
describe "#failure_message" do
- before { subject.matches?(model_class) }
+ before { matcher.matches?(model_class) }
- it { subject.failure_message.should == %{expected #{model_class.name} to include ActiveAttr::AttributeDefaults} }
+ it { matcher.failure_message.should == %{expected #{model_class.name} to include ActiveAttr::AttributeDefaults} }
end
end
@@ -74,18 +74,18 @@ def self.name
end
describe "#matches?" do
- it { subject.matches?(model_class).should be_false }
+ it { matcher.matches?(model_class).should be_false }
end
describe "#failure_message" do
- before { subject.matches?(model_class) }
+ before { matcher.matches?(model_class) }
- it { subject.failure_message.should == %{expected #{model_class.name} to include ActiveAttr::TypecastedAttributes} }
+ it { matcher.failure_message.should == %{expected #{model_class.name} to include ActiveAttr::TypecastedAttributes} }
end
end
context "a matcher with just an attribute name" do
- subject { described_class.new(:first_name) }
+ subject(:matcher) { described_class.new(:first_name) }
it_should_behave_like "a matcher matching a class without ActiveAttr::Attributes"
@@ -93,14 +93,14 @@ def self.name
before { model_class.attribute :first_name }
describe "#matches?" do
- it { subject.matches?(model_class).should be_true }
+ it { matcher.matches?(model_class).should be_true }
end
describe "#negative_failure_message" do
- before { subject.matches?(model_class) }
+ before { matcher.matches?(model_class) }
it do
- subject.negative_failure_message.should == <<-MESSAGE.strip_heredoc.chomp
+ matcher.negative_failure_message.should == <<-MESSAGE.strip_heredoc.chomp
expected not: attribute :first_name
got: attribute :first_name
MESSAGE
@@ -110,19 +110,19 @@ def self.name
context "a class without the attribute" do
describe "#matches?" do
- it { subject.matches?(model_class).should be_false }
+ it { matcher.matches?(model_class).should be_false }
end
describe "#failure_message" do
- before { subject.matches?(model_class) }
+ before { matcher.matches?(model_class) }
- it { subject.failure_message.should == %{expected Person to have attribute named first_name} }
+ it { matcher.failure_message.should == %{expected Person to have attribute named first_name} }
end
end
end
context "a matcher with a default value" do
- subject { described_class.new(:first_name).with_default_value_of("John") }
+ subject(:matcher) { described_class.new(:first_name).with_default_value_of("John") }
it_should_behave_like "a matcher matching a class without ActiveAttr::Attributes"
it_should_behave_like "a matcher matching a class without ActiveAttr::AttributeDefaults"
@@ -131,14 +131,14 @@ def self.name
before { model_class.attribute :first_name }
describe "#matches?" do
- it { subject.matches?(model_class).should be_false }
+ it { matcher.matches?(model_class).should be_false }
end
describe "#failure_message" do
- before { subject.matches?(model_class) }
+ before { matcher.matches?(model_class) }
it do
- subject.failure_message.should == <<-MESSAGE.strip_heredoc.chomp
+ matcher.failure_message.should == <<-MESSAGE.strip_heredoc.chomp
expected: attribute :first_name, :default => "John"
got: attribute :first_name
MESSAGE
@@ -150,14 +150,14 @@ def self.name
before { model_class.attribute :first_name, :default => "Doe" }
describe "#matches?" do
- it { subject.matches?(model_class).should be_false }
+ it { matcher.matches?(model_class).should be_false }
end
describe "#failure_message" do
- before { subject.matches?(model_class) }
+ before { matcher.matches?(model_class) }
it do
- subject.failure_message.should == <<-MESSAGE.strip_heredoc.chomp
+ matcher.failure_message.should == <<-MESSAGE.strip_heredoc.chomp
expected: attribute :first_name, :default => "John"
got: attribute :first_name, :default => "Doe"
MESSAGE
@@ -169,14 +169,14 @@ def self.name
before { model_class.attribute :first_name, :default => "John" }
describe "#matches?" do
- it { subject.matches?(model_class).should be_true }
+ it { matcher.matches?(model_class).should be_true }
end
describe "#negative_failure_message" do
- before { subject.matches?(model_class) }
+ before { matcher.matches?(model_class) }
it do
- subject.negative_failure_message.should == <<-MESSAGE.strip_heredoc.chomp
+ matcher.negative_failure_message.should == <<-MESSAGE.strip_heredoc.chomp
expected not: attribute :first_name, :default => "John"
got: attribute :first_name, :default => "John"
MESSAGE
@@ -186,7 +186,7 @@ def self.name
end
context "a matcher with a default value of false" do
- subject { described_class.new(:admin).with_default_value_of(false) }
+ subject(:matcher) { described_class.new(:admin).with_default_value_of(false) }
it_should_behave_like "a matcher matching a class without ActiveAttr::Attributes"
it_should_behave_like "a matcher matching a class without ActiveAttr::AttributeDefaults"
@@ -195,14 +195,14 @@ def self.name
before { model_class.attribute :admin }
describe "#matches?" do
- it { subject.matches?(model_class).should be_false }
+ it { matcher.matches?(model_class).should be_false }
end
describe "#failure_message" do
- before { subject.matches?(model_class) }
+ before { matcher.matches?(model_class) }
it do
- subject.failure_message.should == <<-MESSAGE.strip_heredoc.chomp
+ matcher.failure_message.should == <<-MESSAGE.strip_heredoc.chomp
expected: attribute :admin, :default => false
got: attribute :admin
MESSAGE
@@ -214,14 +214,14 @@ def self.name
before { model_class.attribute :admin, :default => nil }
describe "#matches?" do
- it { subject.matches?(model_class).should be_false }
+ it { matcher.matches?(model_class).should be_false }
end
describe "#failure_message" do
- before { subject.matches?(model_class) }
+ before { matcher.matches?(model_class) }
it do
- subject.failure_message.should == <<-MESSAGE.strip_heredoc.chomp
+ matcher.failure_message.should == <<-MESSAGE.strip_heredoc.chomp
expected: attribute :admin, :default => false
got: attribute :admin, :default => nil
MESSAGE
@@ -233,14 +233,14 @@ def self.name
before { model_class.attribute :admin, :default => false }
describe "#matches?" do
- it { subject.matches?(model_class).should be_true }
+ it { matcher.matches?(model_class).should be_true }
end
describe "#negative_failure_message" do
- before { subject.matches?(model_class) }
+ before { matcher.matches?(model_class) }
it do
- subject.negative_failure_message.should == <<-MESSAGE.strip_heredoc.chomp
+ matcher.negative_failure_message.should == <<-MESSAGE.strip_heredoc.chomp
expected not: attribute :admin, :default => false
got: attribute :admin, :default => false
MESSAGE
@@ -250,7 +250,7 @@ def self.name
end
context "a matcher with a default value of nil" do
- subject { described_class.new(:first_name).with_default_value_of(nil) }
+ subject(:matcher) { described_class.new(:first_name).with_default_value_of(nil) }
it_should_behave_like "a matcher matching a class without ActiveAttr::Attributes"
it_should_behave_like "a matcher matching a class without ActiveAttr::AttributeDefaults"
@@ -259,14 +259,14 @@ def self.name
before { model_class.attribute :first_name }
describe "#matches?" do
- it { subject.matches?(model_class).should be_true }
+ it { matcher.matches?(model_class).should be_true }
end
describe "#negative_failure_message" do
- before { subject.matches?(model_class) }
+ before { matcher.matches?(model_class) }
it do
- subject.negative_failure_message.should == <<-MESSAGE.strip_heredoc.chomp
+ matcher.negative_failure_message.should == <<-MESSAGE.strip_heredoc.chomp
expected not: attribute :first_name, :default => nil
got: attribute :first_name
MESSAGE
@@ -278,14 +278,14 @@ def self.name
before { model_class.attribute :first_name, :default => false }
describe "#matches?" do
- it { subject.matches?(model_class).should be_false }
+ it { matcher.matches?(model_class).should be_false }
end
describe "#failure_message" do
- before { subject.matches?(model_class) }
+ before { matcher.matches?(model_class) }
it do
- subject.failure_message.should == <<-MESSAGE.strip_heredoc.chomp
+ matcher.failure_message.should == <<-MESSAGE.strip_heredoc.chomp
expected: attribute :first_name, :default => nil
got: attribute :first_name, :default => false
MESSAGE
@@ -297,14 +297,14 @@ def self.name
before { model_class.attribute :first_name, :default => nil }
describe "#matches?" do
- it { subject.matches?(model_class).should be_true }
+ it { matcher.matches?(model_class).should be_true }
end
describe "#negative_failure_message" do
- before { subject.matches?(model_class) }
+ before { matcher.matches?(model_class) }
it do
- subject.negative_failure_message.should == <<-MESSAGE.strip_heredoc.chomp
+ matcher.negative_failure_message.should == <<-MESSAGE.strip_heredoc.chomp
expected not: attribute :first_name, :default => nil
got: attribute :first_name, :default => nil
MESSAGE
@@ -314,7 +314,7 @@ def self.name
end
context "a matcher with a type" do
- subject { described_class.new(:first_name).of_type(String) }
+ subject(:matcher) { described_class.new(:first_name).of_type(String) }
it_should_behave_like "a matcher matching a class without ActiveAttr::Attributes"
it_should_behave_like "a matcher matching a class without ActiveAttr::TypecastedAttributes"
@@ -323,14 +323,14 @@ def self.name
before { model_class.attribute :first_name }
describe "#matches?" do
- it { subject.matches?(model_class).should be_false }
+ it { matcher.matches?(model_class).should be_false }
end
describe "#failure_message" do
- before { subject.matches?(model_class) }
+ before { matcher.matches?(model_class) }
it do
- subject.failure_message.should == <<-MESSAGE.strip_heredoc.chomp
+ matcher.failure_message.should == <<-MESSAGE.strip_heredoc.chomp
expected: attribute :first_name, :type => String
got: attribute :first_name
MESSAGE
@@ -342,14 +342,14 @@ def self.name
before { model_class.attribute :first_name, :type => Symbol }
describe "#matches?" do
- it { subject.matches?(model_class).should be_false }
+ it { matcher.matches?(model_class).should be_false }
end
describe "#failure_message" do
- before { subject.matches?(model_class) }
+ before { matcher.matches?(model_class) }
it do
- subject.failure_message.should == <<-MESSAGE.strip_heredoc.chomp
+ matcher.failure_message.should == <<-MESSAGE.strip_heredoc.chomp
expected: attribute :first_name, :type => String
got: attribute :first_name, :type => Symbol
MESSAGE
@@ -361,14 +361,14 @@ def self.name
before { model_class.attribute :first_name, :type => String }
describe "#matches?" do
- it { subject.matches?(model_class).should be_true }
+ it { matcher.matches?(model_class).should be_true }
end
describe "#negative_failure_message" do
- before { subject.matches?(model_class) }
+ before { matcher.matches?(model_class) }
it do
- subject.negative_failure_message.should == <<-MESSAGE.strip_heredoc.chomp
+ matcher.negative_failure_message.should == <<-MESSAGE.strip_heredoc.chomp
expected not: attribute :first_name, :type => String
got: attribute :first_name, :type => String
MESSAGE
@@ -378,7 +378,7 @@ def self.name
end
context "a matcher with a type of Object" do
- subject { described_class.new(:first_name).of_type(Object) }
+ subject(:matcher) { described_class.new(:first_name).of_type(Object) }
it_should_behave_like "a matcher matching a class without ActiveAttr::Attributes"
it_should_behave_like "a matcher matching a class without ActiveAttr::TypecastedAttributes"
@@ -387,14 +387,14 @@ def self.name
before { model_class.attribute :first_name }
describe "#matches?" do
- it { subject.matches?(model_class).should be_true }
+ it { matcher.matches?(model_class).should be_true }
end
describe "#negative_failure_message" do
- before { subject.matches?(model_class) }
+ before { matcher.matches?(model_class) }
it do
- subject.negative_failure_message.should == <<-MESSAGE.strip_heredoc.chomp
+ matcher.negative_failure_message.should == <<-MESSAGE.strip_heredoc.chomp
expected not: attribute :first_name, :type => Object
got: attribute :first_name
MESSAGE
@@ -406,14 +406,14 @@ def self.name
before { model_class.attribute :first_name, :type => String }
describe "#matches?" do
- it { subject.matches?(model_class).should be_false }
+ it { matcher.matches?(model_class).should be_false }
end
describe "#failure_message" do
- before { subject.matches?(model_class) }
+ before { matcher.matches?(model_class) }
it do
- subject.failure_message.should == <<-MESSAGE.strip_heredoc.chomp
+ matcher.failure_message.should == <<-MESSAGE.strip_heredoc.chomp
expected: attribute :first_name, :type => Object
got: attribute :first_name, :type => String
MESSAGE
@@ -425,14 +425,14 @@ def self.name
before { model_class.attribute :first_name, :type => Object }
describe "#matches?" do
- it { subject.matches?(model_class).should be_true }
+ it { matcher.matches?(model_class).should be_true }
end
describe "#negative_failure_message" do
- before { subject.matches?(model_class) }
+ before { matcher.matches?(model_class) }
it do
- subject.negative_failure_message.should == <<-MESSAGE.strip_heredoc.chomp
+ matcher.negative_failure_message.should == <<-MESSAGE.strip_heredoc.chomp
expected not: attribute :first_name, :type => Object
got: attribute :first_name, :type => Object
MESSAGE
View
41 spec/functional/active_attr/model_spec.rb
@@ -21,19 +21,22 @@ def self.name
end
end
- subject { model_class.new }
- it_should_behave_like "ActiveModel"
+ subject(:model) { model_class.new }
+
+ it_should_behave_like "ActiveModel" do
+ subject { model_class.new }
+ end
it "reads and writes attributes" do
- subject.first_name = "Chris"
- subject.first_name.should eq "Chris"
- subject.attributes["first_name"].should == "Chris"
+ model.first_name = "Chris"
+ model.first_name.should eq "Chris"
+ model.attributes["first_name"].should == "Chris"
end
it "can query attributes" do
- subject.first_name?.should be_false
- subject.first_name = "Chris"
- subject.first_name?.should be_true
+ model.first_name?.should be_false
+ model.first_name = "Chris"
+ model.first_name?.should be_true
end
it "initializes with a block that can use attributes" do
@@ -51,14 +54,14 @@ def self.name
it "has a logger" do
logger = double("logger")
model_class.logger?.should be_false
- subject.logger?.should be_false
+ model.logger?.should be_false
model_class.logger = logger
model_class.logger?.should be_true
model_class.logger.should eq logger
- subject.logger?.should be_true
- subject.logger.should == logger
+ model.logger?.should be_true
+ model.logger.should == logger
end
it "supports mass assignment with security" do
@@ -68,17 +71,17 @@ def self.name
end
it "does not use strict mass assignment" do
- expect { subject.assign_attributes :middle_initial => "J" }.not_to raise_error
+ expect { model.assign_attributes :middle_initial => "J" }.not_to raise_error
end
it "serializes to/from JSON" do
- subject.first_name = "Chris"
- model_class.new.from_json(subject.to_json).first_name.should == "Chris"
+ model.first_name = "Chris"
+ model_class.new.from_json(model.to_json).first_name.should == "Chris"
end
it "serializes to/from XML" do
- subject.first_name = "Chris"
- model_class.new.from_xml(subject.to_xml).first_name.should == "Chris"
+ model.first_name = "Chris"
+ model_class.new.from_xml(model.to_xml).first_name.should == "Chris"
end
it "supports attribute name translation" do
@@ -86,8 +89,8 @@ def self.name
end
it "typecasts attributes" do
- subject.age = "29"
- subject.age.should eql 29
+ model.age = "29"
+ model.age.should eql 29
end
context "attribute defaults" do
@@ -102,7 +105,7 @@ def self.name
end
it "are applied" do
- subject.age_limit.should == 21
+ model.age_limit.should == 21
end
it "are overridden by mass assigned attributes" do
View
10 spec/functional/active_attr/serialization_spec.rb
@@ -16,16 +16,16 @@ def self.name
end
end
- subject { model_class.new }
+ subject(:model) { model_class.new }
it "serializes to/from JSON" do
- subject.first_name = "Chris"
- model_class.new.from_json(subject.to_json).first_name.should == "Chris"
+ model.first_name = "Chris"
+ model_class.new.from_json(model.to_json).first_name.should == "Chris"
end
it "serializes to/from XML" do
- subject.first_name = "Chris"
- model_class.new.from_xml(subject.to_xml).first_name.should == "Chris"
+ model.first_name = "Chris"
+ model_class.new.from_xml(model.to_xml).first_name.should == "Chris"
end
end
end
View
90 spec/functional/active_attr/typecasted_attributes_spec.rb
@@ -3,7 +3,7 @@
module ActiveAttr
describe TypecastedAttributes do
- subject { model_class.new }
+ subject(:model) { model_class.new }
let :model_class do
Class.new do
@@ -30,118 +30,118 @@ def self.to_s
context "when assigning nil" do
it "a typeless attribute returns nil" do
- subject.typeless = nil
- subject.typeless.should be_nil
+ model.typeless = nil
+ model.typeless.should be_nil
end
it "an attribute with no known typecaster raises" do
- subject.unknown = nil
- expect { subject.unknown }.to raise_error Typecasting::UnknownTypecasterError, "Unable to cast to type Unknown"
+ model.unknown = nil
+ expect { model.unknown }.to raise_error Typecasting::UnknownTypecasterError, "Unable to cast to type Unknown"
end
it "an attribute with an inline typecaster returns nil" do
- subject.age = nil
- subject.age.should be_nil
+ model.age = nil
+ model.age.should be_nil
end
it "an Object attribute returns nil" do
- subject.object = nil
- subject.object.should be_nil
+ model.object = nil
+ model.object.should be_nil
end
it "a BigDecimal attribute returns nil" do
- subject.big_decimal = nil
- subject.big_decimal.should be_nil
+ model.big_decimal = nil
+ model.big_decimal.should be_nil
end
it "a Boolean attribute returns nil" do
- subject.boolean = nil
- subject.boolean.should be_nil
+ model.boolean = nil
+ model.boolean.should be_nil
end
it "a Date attribute returns nil" do
- subject.date = nil
- subject.date.should be_nil
+ model.date = nil
+ model.date.should be_nil
end
it "a DateTime attribute returns nil" do
- subject.date_time = nil
- subject.date_time.should be_nil
+ model.date_time = nil
+ model.date_time.should be_nil
end
it "a Float attribute returns nil" do
- subject.float = nil
- subject.float.should be_nil
+ model.float = nil
+ model.float.should be_nil
end
it "an Integer attribute returns nil" do
- subject.integer = nil
- subject.integer.should be_nil
+ model.integer = nil
+ model.integer.should be_nil
end
it "a String attribute returns nil" do
- subject.string = nil
- subject.string.should be_nil
+ model.string = nil
+ model.string.should be_nil
end
end
context "when assigning a valid String" do
it "a typeless attribute returns the original String" do
value = "test"
- subject.typeless = value
- subject.typeless.should equal value
+ model.typeless = value
+ model.typeless.should equal value
end
it "an Object attribute returns the original String" do
value = "test"
- subject.object = value
- subject.object.should equal value
+ model.object = value
+ model.object.should equal value
end
it "a BigDecimal attribute returns a BigDecimal" do
- subject.big_decimal = "1.1"
- subject.big_decimal.should eql BigDecimal.new("1.1")
+ model.big_decimal = "1.1"
+ model.big_decimal.should eql BigDecimal.new("1.1")
end
it "a Boolean attribute returns a Boolean" do
- subject.boolean = "false"
- subject.boolean.should eql false
+ model.boolean = "false"
+ model.boolean.should eql false
end
it "a Date attribute returns a Date" do
- subject.date = "2012-01-01"
- subject.date.should eql Date.new(2012, 1, 1)
+ model.date = "2012-01-01"
+ model.date.should eql Date.new(2012, 1, 1)
end
it "a Date attribute before typecasting returns the original String" do
value = "2012-01-01"
- subject.date = value
- subject.date_before_type_cast.should equal value
+ model.date = value
+ model.date_before_type_cast.should equal value
end
it "a DateTime attribute returns a DateTime" do
- subject.date_time = "2012-01-01"
- subject.date_time.should eql DateTime.new(2012, 1, 1)
+ model.date_time = "2012-01-01"
+ model.date_time.should eql DateTime.new(2012, 1, 1)
end
it "a Float attribute returns a Float" do
- subject.float = "1.1"
- subject.float.should eql 1.1
+ model.float = "1.1"
+ model.float.should eql 1.1
end
it "an Integer attribute returns an Integer" do
- subject.integer = "1"
- subject.integer.should eql 1
+ model.integer = "1"
+ model.integer.should eql 1
end
it "a String attribute returns the String" do
- subject.string = "1.0"
- subject.string.should eql "1.0"
+ model.string = "1.0"
+ model.string.should eql "1.0"
end
it "an attribute using an inline typecaster returns the result of the inline typecaster" do
- subject.age = 2
- subject.age.should == Age.new(2)
+ model.age = 2
+ model.age.should == Age.new(2)
end
end
end
View
24 spec/support/mass_assignment_shared_examples.rb
@@ -12,7 +12,7 @@ def name=(name)
end
end
- let(:person) { subject.new }
+ let(:person) { model_class.new }
let(:first_name) { "Chris" }
let(:last_name) { "Griego" }
@@ -52,19 +52,19 @@ def mass_assign_attributes_with_options(attributes, options)
end
it "raises ArgumentError when called with three arguments" do
- expect { subject.new.assign_attributes({}, {}, nil) }.to raise_error ArgumentError
+ expect { model_class.new.assign_attributes({}, {}, nil) }.to raise_error ArgumentError
end
it "does not raise when called with two arguments" do
- expect { subject.new.assign_attributes({}, {}) }.not_to raise_error
+ expect { model_class.new.assign_attributes({}, {}) }.not_to raise_error
end
it "does not raise when called with a single argument" do
- expect { subject.new.assign_attributes({}) }.not_to raise_error
+ expect { model_class.new.assign_attributes({}) }.not_to raise_error
end
it "raises ArgumentError when called with no arguments" do
- expect { subject.new.assign_attributes }.to raise_error ArgumentError
+ expect { model_class.new.assign_attributes }.to raise_error ArgumentError
end
end
@@ -81,30 +81,30 @@ def mass_assign_attributes(attributes)
shared_examples "#initialize", :initialize => true do
def mass_assign_attributes(attributes)
- subject.new(attributes)
+ model_class.new(attributes)
end
def mass_assign_attributes_with_options(attributes, options)
- subject.new(attributes, options)
+ model_class.new(attributes, options)
end
it "invokes the superclass initializer" do
- subject.new.should be_initialized
+ model_class.new.should be_initialized
end
it "raises ArgumentError when called with three arguments" do
- expect { subject.new({}, {}, nil) }.to raise_error ArgumentError
+ expect { model_class.new({}, {}, nil) }.to raise_error ArgumentError
end
it "does not raise when called with two arguments" do
- expect { subject.new({}, {}) }.not_to raise_error
+ expect { model_class.new({}, {}) }.not_to raise_error
end
it "does not raise when called with a single argument" do
- expect { subject.new({}) }.not_to raise_error
+ expect { model_class.new({}) }.not_to raise_error
end
it "does not raise when called with no arguments" do
- expect { subject.new }.not_to raise_error
+ expect { model_class.new }.not_to raise_error
end
end
View
12 spec/unit/active_attr/attribute_defaults_spec.rb
@@ -3,7 +3,7 @@
module ActiveAttr
describe AttributeDefaults do
- subject { model_class.new }
+ subject(:model) { model_class.new }
let :model_class do
Class.new do
@@ -16,21 +16,21 @@ module ActiveAttr
end
describe "#attribute_defaults" do
- subject { model_class.new.attribute_defaults }
+ subject(:attribute_defaults) { model_class.new.attribute_defaults }
it { should be_a_kind_of Hash }
it "includes declared literal string attribute defaults" do
- subject["first_name"].should == "John"
+ attribute_defaults["first_name"].should == "John"
end
it "includes declared nil attribute defaults" do
- subject.should include "age"
- subject["age"].should be_nil
+ attribute_defaults.should include "age"
+ attribute_defaults["age"].should be_nil
end
it "includes declared dynamic attribute defaults" do
- subject["created_at"].should be_a_kind_of Time
+ attribute_defaults["created_at"].should be_a_kind_of Time
end
end
View
12 spec/unit/active_attr/attribute_definition_spec.rb
@@ -3,15 +3,15 @@
module ActiveAttr
describe AttributeDefinition do
- subject { described_class.new(:amount, :default => "default") }
+ subject(:attribute_definition) { described_class.new(:amount, :default => "default") }
describe "#<=>" do
it "is nil if the right side is not an #{described_class}" do
- (subject <=> nil).should be_nil
+ (attribute_definition <=> nil).should be_nil
end
it "prefers neither when both sides use the same attribute name and options" do
- (subject <=> subject).should == 0
+ (attribute_definition <=> attribute_definition).should == 0
end
it "prefers the left side when the left side name sorts alphabetically before the right side name" do
@@ -39,7 +39,7 @@ module ActiveAttr
describe "#[]" do
it "reads an attribute option" do
- subject[:default].should == "default"
+ attribute_definition[:default].should == "default"
end
end
@@ -91,13 +91,13 @@ module ActiveAttr
describe "#to_s" do
it "renders the name as a String" do
- subject.to_s.should == "amount"
+ attribute_definition.to_s.should == "amount"
end
end
describe "#to_sym" do
it "renders the name as a Symbol" do
- subject.to_sym.should == :amount
+ attribute_definition.to_sym.should == :amount
end
end
end
View
76 spec/unit/active_attr/attributes_spec.rb
@@ -3,7 +3,7 @@
module ActiveAttr
describe Attributes do
- subject { model_class.new }
+ subject(:model) { model_class.new }
let(:last_name) { "Poweski" }
let :model_class do
@@ -69,23 +69,23 @@ def self.name
end
it "defines an attribute reader that calls #attribute" do
- subject.should_receive(:attribute).with("first_name")
- subject.first_name
+ model.should_receive(:attribute).with("first_name")
+ model.first_name
end
it "defines an attribute reader that can be called via super" do
- subject.should_receive(:attribute).with("amount")
- subject.amount
+ model.should_receive(:attribute).with("amount")
+ model.amount
end
it "defines an attribute writer that calls #attribute=" do
- subject.should_receive(:attribute=).with("first_name", "Ben")
- subject.first_name = "Ben"
+ model.should_receive(:attribute=).with("first_name", "Ben")
+ model.first_name = "Ben"
end
it "defines an attribute writer that can be called via super" do
- subject.should_receive(:attribute=).with("amount", 1)
- subject.amount = 1
+ model.should_receive(:attribute=).with("amount", 1)
+ model.amount = 1
end
it "defining an attribute twice does not give the class two attribute definitions" do
@@ -121,17 +121,17 @@ def self.name
it "defines an attribute reader that calls #attribute" do
attributeless.attribute! :first_name
- instance = attributeless.new
+ model = attributeless.new
result = mock
- instance.should_receive(:attribute).with("first_name").and_return(result)
- instance.first_name.should equal result
+ model.should_receive(:attribute).with("first_name").and_return(result)
+ model.first_name.should equal result
end
it "defines an attribute writer that calls #attribute=" do
attributeless.attribute! :first_name
- instance = attributeless.new
- instance.should_receive(:attribute=).with("first_name", "Ben")
- instance.first_name = "Ben"
+ model = attributeless.new
+ model.should_receive(:attribute=).with("first_name", "Ben")
+ model.first_name = "Ben"
end
end
@@ -186,32 +186,32 @@ def self.name
context "when an attribute is defined" do
it "returns the key value pairs" do
- subject.first_name = "Ben"
- subject.attributes.should include("first_name" => "Ben")
+ model.first_name = "Ben"
+ model.attributes.should include("first_name" => "Ben")
end
it "returns a new Hash " do
- subject.attributes.merge!("first_name" => "Bob")
- subject.attributes.should_not include("first_name" => "Bob")
+ model.attributes.merge!("first_name" => "Bob")
+ model.attributes.should_not include("first_name" => "Bob")
end
it "returns all attributes" do
- subject.attributes.keys.should =~ %w(amount first_name last_name)
+ model.attributes.keys.should =~ %w(amount first_name last_name)
end
end
context "when a getter is overridden" do
it "uses the overridden implementation" do
- subject.attributes.should include("last_name" => last_name)
+ model.attributes.should include("last_name" => last_name)
end
end
end
describe "#inspect" do
- before { subject.first_name = "Ben" }
+ before { model.first_name = "Ben" }
it "includes the class name and all attribute values in alphabetical order by attribute name" do
- subject.inspect.should == %{#<Foo amount: nil, first_name: "Ben", last_name: "#{last_name}">}
+ model.inspect.should == %{#<Foo amount: nil, first_name: "Ben", last_name: "#{last_name}">}
end
it "doesn't format the inspection string for attributes if the model does not have any" do
@@ -220,7 +220,7 @@ def self.name
context "when a getter is overridden" do
it "uses the overridden implementation" do
- subject.inspect.should include %{last_name: "#{last_name}"}
+ model.inspect.should include %{last_name: "#{last_name}"}
end
end
end
@@ -229,33 +229,33 @@ def self.name
describe "##{method}" do
context "when an attribute is not set" do
it "returns nil" do
- subject.send(method, :first_name).should be_nil
+ model.send(method, :first_name).should be_nil
end
end
context "when an attribute is set" do
let(:first_name) { "Bob" }
- before { subject.write_attribute(:first_name, first_name) }
+ before { model.write_attribute(:first_name, first_name) }
it "returns the attribute using a Symbol" do
- subject.send(method, :first_name).should == first_name
+ model.send(method, :first_name).should == first_name
end
it "returns the attribute using a String" do
- subject.send(method, 'first_name').should == first_name
+ model.send(method, 'first_name').should == first_name
end
end
context "when the getter is overridden" do
it "uses the overridden implementation" do
- subject.send(method, :last_name).should == last_name
+ model.send(method, :last_name).should == last_name
end
end
it "raises when getting an undefined attribute" do
expect do
- subject.send(method, :initials)
+ model.send(method, :initials)
end.to raise_error UnknownAttributeError, "unknown attribute: initials"
end
end
@@ -264,33 +264,33 @@ def self.name
[:[]=, :write_attribute].each do |method|
describe "##{method}" do
it "raises ArgumentError with one argument" do
- expect { subject.send(method, :first_name) }.to raise_error(ArgumentError)
+ expect { model.send(method, :first_name) }.to raise_error(ArgumentError)
end
it "raises ArgumentError with no arguments" do
- expect { subject.send(method) }.to raise_error(ArgumentError)
+ expect { model.send(method) }.to raise_error(ArgumentError)
end
it "sets an attribute using a Symbol and value" do
- expect { subject.send(method, :first_name, "Ben") }.to change { subject.attributes["first_name"] }.from(nil).to("Ben")
+ expect { model.send(method, :first_name, "Ben") }.to change { model.attributes["first_name"] }.from(nil).to("Ben")
end
it "sets an attribute using a String and value" do
- expect { subject.send(method, 'first_name', "Ben") }.to change { subject.attributes["first_name"] }.from(nil).to("Ben")
+ expect { model.send(method, 'first_name', "Ben") }.to change { model.attributes["first_name"] }.from(nil).to("Ben")
end
it "is able to set an attribute to nil" do
- subject.first_name = "Ben"
- expect { subject.send(method, :first_name, nil) }.to change { subject.attributes["first_name"] }.from("Ben").to(nil)
+ model.first_name = "Ben"
+ expect { model.send(method, :first_name, nil) }.to change { model.attributes["first_name"] }.from("Ben").to(nil)
end
it "uses the overridden implementation when the setter is overridden" do
- subject.send(method, :last_name, "poweski").should == "POWESKI"
+ model.send(method, :last_name, "poweski").should == "POWESKI"
end
it "raises when setting an undefined attribute" do
expect do
- subject.send(method, :initials, "BP")
+ model.send(method, :initials, "BP")
end.to raise_error UnknownAttributeError, "unknown attribute: initials"
end
end
View
32 spec/unit/active_attr/logger_spec.rb
@@ -3,7 +3,7 @@
module ActiveAttr
describe Logger do
- subject { model_class.new }
+ subject(:model) { model_class.new }
let(:logger) { double "logger" }
let(:child_class) { Class.new(parent_class) }
let(:parent_class) { model_class }
@@ -32,11 +32,11 @@ module ActiveAttr
end
it "#logger is nil" do
- subject.logger.should be_nil
+ model.logger.should be_nil
end
it "#logger? is false" do
- subject.logger?.should == false
+ model.logger?.should == false
end
end
@@ -61,11 +61,11 @@ module ActiveAttr
end
it "#logger is the logger" do
- subject.logger.should == logger
+ model.logger.should == logger
end
it "#logger? is true" do
- subject.logger?.should == true
+ model.logger?.should == true
end
end
@@ -89,17 +89,17 @@ module ActiveAttr
end
it "#logger is the logger" do
- subject.logger.should == logger
+ model.logger.should == logger
end
it "#logger? is true" do
- subject.logger?.should == true
+ model.logger?.should == true
end
end
context "when the logger is set on a parent class" do
before { parent_class.logger = logger }
- subject { child_class.new }
+ subject(:model) { child_class.new }
it "#{described_class}.logger is nil" do
described_class.logger.should be_nil
@@ -118,17 +118,17 @@ module ActiveAttr
end
it "#logger is the logger" do
- subject.logger.should == logger
+ model.logger.should == logger
end
it "#logger? is true" do
- subject.logger?.should == true
+ model.logger?.should == true
end
end
context "when the logger is set on a child class" do
before { child_class.logger = logger }
- subject { parent_class.new }
+ subject(:model) { parent_class.new }
it "#{described_class}.logger is nil" do
described_class.logger.should be_nil
@@ -147,16 +147,16 @@ module ActiveAttr
end
it "#logger is nil" do
- subject.logger.should be_nil
+ model.logger.should be_nil
end
it "#logger? is false" do
- subject.logger?.should == false
+ model.logger?.should == false
end
end
context "when the logger is set on the instance" do
- before { subject.logger = logger }
+ before { model.logger = logger }
it "#{described_class}.logger is nil" do
described_class.logger.should be_nil
@@ -175,11 +175,11 @@ module ActiveAttr
end
it "#logger is the logger" do
- subject.logger.should == logger
+ model.logger.should == logger
end
it "#logger? is true" do
- subject.logger?.should == true
+ model.logger?.should == true
end
end
end
View
2  spec/unit/active_attr/mass_assignment_security_spec.rb
@@ -3,8 +3,6 @@
module ActiveAttr
describe MassAssignmentSecurity, :mass_assignment do
- subject { model_class }
-
before do
model_class.class_eval do
include MassAssignmentSecurity
View
2  spec/unit/active_attr/mass_assignment_spec.rb
@@ -3,7 +3,7 @@
module ActiveAttr
describe MassAssignment, :mass_assignment do
- subject do
+ before do
model_class.class_eval do
include MassAssignment
end
View
7 spec/unit/active_attr/matchers_spec.rb
@@ -3,20 +3,19 @@
module ActiveAttr
describe Matchers do
- let(:dsl) { Object.new.extend described_class }
- subject { dsl }
+ subject(:dsl) { Object.new.extend described_class }
it { should respond_to(:have_attribute).with(1).argument }
describe "#have_attribute" do
- subject { dsl.have_attribute(:first_name) }
+ subject(:matcher) { dsl.have_attribute(:first_name) }
it "builds a HaveAttributeMatcher" do
should be_a_kind_of Matchers::HaveAttributeMatcher
end
it "uses the given attribute name to construct the matcher" do
- subject.send(:attribute_name).should == :first_name
+ matcher.send(:attribute_name).should == :first_name
end
end
end
View
150 spec/unit/active_attr/query_attributes_spec.rb
@@ -4,7 +4,7 @@
module ActiveAttr
describe QueryAttributes do
- subject { model_class.new }
+ subject(:model) { model_class.new }
let :model_class do
Class.new do
@@ -29,196 +29,196 @@ def false?
describe ".attribute" do
it "defines an attribute predicate method that calls #attribute?" do
- subject.should_receive(:attribute?).with("value")
- subject.value?
+ model.should_receive(:attribute?).with("value")
+ model.value?
end
it "defines an attribute reader that can be called via super" do
- subject.should_receive(:attribute?).with("overridden")
- subject.overridden?
+ model.should_receive(:attribute?).with("overridden")
+ model.overridden?
end
end
describe "#query_attribute" do
it "raises ArgumentError when called with two arguments" do
- expect { subject.query_attribute(:a, :b) }.to raise_error ArgumentError
+ expect { model.query_attribute(:a, :b) }.to raise_error ArgumentError
end
it "does not raise when called with a single argument" do
- expect { subject.query_attribute(:a) }.not_to raise_error ArgumentError
+ expect { model.query_attribute(:a) }.not_to raise_error ArgumentError
end
it "raises ArgumentError when called with no arguments" do
- expect { subject.query_attribute }.to raise_error ArgumentError
+ expect { model.query_attribute }.to raise_error ArgumentError
end
it "calls the predicate method if defined" do
- subject.query_attribute(:true).should eq true
- subject.query_attribute(:false).should == false
+ model.query_attribute(:true).should eq true
+ model.query_attribute(:false).should == false
end
it "raises when getting an undefined attribute" do
- expect { subject.query_attribute(:initials) }.to raise_error UnknownAttributeError, "unknown attribute: initials"
+ expect { model.query_attribute(:initials) }.to raise_error UnknownAttributeError, "unknown attribute: initials"
end
it "is false when the attribute is false" do
- subject.value = false
- subject.value?.should == false
+ model.value = false
+ model.value?.should == false
end
it "is true when the attribute is true" do
- subject.value = true
- subject.value?.should == true
+ model.value = true
+ model.value?.should == true
end
it "is false when the attribute is nil" do
- subject.value = nil
- subject.value?.should == false
+ model.value = nil
+ model.value?.should == false
end
it "is true when the attribute is an Object" do
- subject.value = Object.new
- subject.value?.should == true
+ model.value = Object.new
+ model.value?.should == true
end
it "is false when the attribute is an empty string" do
- subject.value = ""
- subject.value?.should == false
+ model.value = ""
+ model.value?.should == false
end
it "is true when the attribute is a non-empty string" do
- subject.value = "Chris"
- subject.value?.should == true
+ model.value = "Chris"
+ model.value?.should == true
end
it "is false when the attribute is 0" do
- subject.value = 0
- subject.value?.should == false
+ model.value = 0
+ model.value?.should == false
end
it "is true when the attribute is 1" do
- subject.value = 1
- subject.value?.should == true
+ model.value = 1
+ model.value?.should == true
end
it "is false when the attribute is 0.0" do
- subject.value = 0.0
- subject.value?.should == false
+ model.value = 0.0
+ model.value?.should == false
end
it "is true when the attribute is 0.1" do
- subject.value = 0.1
- subject.value?.should == true
+ model.value = 0.1
+ model.value?.should == true
end
it "is false when the attribute is a zero BigDecimal" do
- subject.value = BigDecimal.new("0.0")
- subject.value?.should == false
+ model.value = BigDecimal.new("0.0")
+ model.value?.should == false
end
it "is true when the attribute is a non-zero BigDecimal" do
- subject.value = BigDecimal.new("0.1")
- subject.value?.should == true
+ model.value = BigDecimal.new("0.1")
+ model.value?.should == true
end
it "is true when the attribute is -1" do
- subject.value = -1
- subject.value?.should == true
+ model.value = -1
+ model.value?.should == true
end
it "is false when the attribute is -0.0" do
- subject.value = -0.0
- subject.value?.should == false
+ model.value = -0.0
+ model.value?.should == false
end
it "is true when the attribute is -0.1" do
- subject.value = -0.1
- subject.value?.should == true
+ model.value = -0.1
+ model.value?.should == true
end
it "is false when the attribute is a negative zero BigDecimal" do
- subject.value = BigDecimal.new("-0.0")
- subject.value?.should == false
+ model.value = BigDecimal.new("-0.0")
+ model.value?.should == false
end
it "is true when the attribute is a negative BigDecimal" do
- subject.value = BigDecimal.new("-0.1")
- subject.value?.should == true
+ model.value = BigDecimal.new("-0.1")
+ model.value?.should == true
end
it "is false when the attribute is '0'" do
- subject.value = "0"
- subject.value?.should == false
+ model.value = "0"
+ model.value?.should == false
end
it "is true when the attribute is '1'" do
- subject.value = "1"
- subject.value?.should == true
+ model.value = "1"
+ model.value?.should == true
end
it "is false when the attribute is '0.0'" do
- subject.value = "0.0"
- subject.value?.should == false
+ model.value = "0.0"
+ model.value?.should == false
end
it "is true when the attribute is '0.1'" do
- subject.value = "0.1"
- subject.value?.should == true
+ model.value = "0.1"
+ model.value?.should == true
end
it "is true when the attribute is '-1'" do
- subject.value = "-1"
- subject.value?.should == true
+ model.value = "-1"
+ model.value?.should == true
end
it "is false when the attribute is '-0.0'" do
- subject.value = "-0.0"
- subject.value?.should == false
+ model.value = "-0.0"
+ model.value?.should == false
end
it "is true when the attribute is '-0.1'" do
- subject.value = "-0.1"
- subject.value?.should == true
+ model.value = "-0.1"
+ model.value?.should == true
end
it "is true when the attribute is 'true'" do
- subject.value = "true"
- subject.value?.should == true
+ model.value = "true"
+ model.value?.should == true
end
it "is false when the attribute is 'false'" do
- subject.value = "false"
- subject.value?.should == false
+ model.value = "false"
+ model.value?.should == false
end
it "is true when the attribute is 't'" do
- subject.value = "t"
- subject.value?.should == true
+ model.value = "t"
+ model.value?.should == true
end
it "is false when the attribute is 'f'" do
- subject.value = "f"
- subject.value?.should == false
+ model.value = "f"
+ model.value?.should == false
end
it "is true when the attribute is 'T'" do
- subject.value = "T"
- subject.value?.should == true
+ model.value = "T"
+ model.value?.should == true
end
it "is false when the attribute is 'F'" do
- subject.value = "F"
- subject.value?.should == false
+ model.value = "F"
+ model.value?.should == false
end
it "is true when the attribute is 'TRUE'" do
- subject.value = "TRUE"
- subject.value?.should == true
+ model.value = "TRUE"
+ model.value?.should == true
end
it "is false when the attribute is 'FALSE" do
- subject.value = "FALSE"
- subject.value?.should == false
+ model.value = "FALSE"
+ model.value?.should == false
end
end
end
View
20 spec/unit/active_attr/typecasted_attributes_spec.rb
@@ -3,7 +3,7 @@
module ActiveAttr
describe TypecastedAttributes do
- subject { model_class.new }
+ subject(:model) { model_class.new }
let :model_class do
Class.new do
@@ -35,13 +35,13 @@ def self.name
describe ".attribute" do
it "defines an attribute pre-typecasting reader that calls #attribute_before_type_cast" do
- subject.should_receive(:attribute_before_type_cast).with("first_name")
- subject.first_name_before_type_cast
+ model.should_receive(:attribute_before_type_cast).with("first_name")
+ model.first_name_before_type_cast
end
it "defines an attribute reader that can be called via super" do
- subject.should_receive(:attribute_before_type_cast).with("last_name")
- subject.last_name_before_type_cast
+ model.should_receive(:attribute_before_type_cast).with("last_name")
+ model.last_name_before_type_cast
end
end
@@ -61,19 +61,19 @@ def self.name
describe "#attribute_before_type_cast" do
it "returns nil when the attribute has not been assigned yet" do
- subject.attribute_before_type_cast(:amount).should be_nil
+ model.attribute_before_type_cast(:amount).should be_nil
end
it "returns the assigned attribute value, without typecasting, when given an attribute name as a Symbol" do
value = :value
- subject.amount = value
- subject.attribute_before_type_cast(:amount).should equal value
+ model.amount = value
+ model.attribute_before_type_cast(:amount).should equal value
end
it "returns the assigned attribute value, without typecasting, when given an attribute name as a String" do
value = :value
- subject.amount = value
- subject.attribute_before_type_cast('amount').should equal value
+ model.amount = value
+ model.attribute_before_type_cast('amount').should equal value
end
end
end
View
16 spec/unit/active_attr/typecasting/big_decimal_typecaster_spec.rb
@@ -4,34 +4,36 @@
module ActiveAttr
module Typecasting
describe BigDecimalTypecaster do
+ subject(:typecaster) { described_class.new }
+
describe "#call" do
it "returns the original BigDecimal for a BigDecimal" do
value = BigDecimal.new("2.0")
- subject.call(value).should equal value
+ typecaster.call(value).should equal value
end
it "casts nil to a zero BigDecimal" do
- subject.call(nil).should eql BigDecimal.new("0.0")
+ typecaster.call(nil).should eql BigDecimal.new("0.0")
end
it "casts a numeric String to a BigDecimal" do
- subject.call("2").should eql BigDecimal.new("2.0")
+ typecaster.call("2").should eql BigDecimal.new("2.0")
end
it "casts a alpha String to a zero BigDecimal" do
- subject.call("bob").should eql BigDecimal.new("0.0")
+ typecaster.call("bob").should eql BigDecimal.new("0.0")
end
it "casts a Rational to a BigDecimal" do
- subject.call(Rational(1, 2)).should eql BigDecimal.new("0.5")
+ typecaster.call(Rational(1, 2)).should eql BigDecimal.new("0.5")
end
it "casts a Float to a BigDecimal" do
- subject.call(1.2).should eql BigDecimal.new("1.2")
+ typecaster.call(1.2).should eql BigDecimal.new("1.2")
end
it "cases an Integer to a BigDecimal" do
- subject.call(2).should eql BigDecimal.new("2.0")
+ typecaster.call(2).should eql BigDecimal.new("2.0")
end
end
end
View
52 spec/unit/active_attr/typecasting/boolean_typecaster_spec.rb
@@ -5,30 +5,32 @@
module ActiveAttr
module Typecasting
describe BooleanTypecaster do
+ subject(:typecaster) { described_class.new }
+
describe "#call" do
it "returns true for true" do
- subject.call(true).should equal true
+ typecaster.call(true).should equal true
end
it "returns false for false" do
- subject.call(false).should equal false
+ typecaster.call(false).should equal false
end
it "casts nil to false" do
- subject.call(nil).should equal false
+ typecaster.call(nil).should equal false
end
it "casts an Object to true" do
- subject.call(Object.new).should equal true
+ typecaster.call(Object.new).should equal true
end
context "when the value is a String" do
it "casts an empty String to false" do
- subject.call("").should equal false
+ typecaster.call("").should equal false
end
it "casts a non-empty String to true" do
- subject.call("abc").should equal true
+ typecaster.call("abc").should equal true
end
{
@@ -61,84 +63,84 @@ module Typecasting
"OFF" => false,
}.each_pair do |value, result|
it "casts #{value.inspect} to #{result.inspect}" do
- subject.call(value).should equal result
+ typecaster.call(value).should equal result
end
end
end
context "when the value is Numeric" do
it "casts 0 to false" do
- subject.call(0).should equal false
+ typecaster.call(0).should equal false
end
it "casts 1 to true" do
- subject.call(1).should equal true
+ typecaster.call(1).should equal true
end
it "casts 0.0 to false" do
- subject.call(0.0).should equal false
+ typecaster.call(0.0).should equal false
end
it "casts 0.1 to true" do
- subject.call(0.1).should equal true
+ typecaster.call(0.1).should equal true
end
it "casts a zero BigDecimal to false" do
- subject.call(BigDecimal.new("0.0")).should equal false
+ typecaster.call(BigDecimal.new("0.0")).should equal false
end
it "casts a non-zero BigDecimal to true" do
- subject.call(BigDecimal.new("0.1")).should equal true
+ typecaster.call(BigDecimal.new("0.1")).should equal true
end
it "casts -1 to true" do
- subject.call(-1).should equal true
+ typecaster.call(-1).should equal true
end
it "casts -0.0 to false" do
- subject.call(-0.0).should equal false
+ typecaster.call(-0.0).should equal false
end
it "casts -0.1 to true" do
- subject.call(-0.1).should equal true
+ typecaster.call(-0.1).should equal true
end
it "casts a negative zero BigDecimal to false" do
- subject.call(BigDecimal.new("-0.0")).should equal false
+ typecaster.call(BigDecimal.new("-0.0")).should equal false
end
it "casts a negative BigDecimal to true" do
- subject.call(BigDecimal.new("-0.1")).should equal true
+ typecaster.call(BigDecimal.new("-0.1")).should equal true
end
end
context "when the value is the String version of a Numeric" do
it "casts '0' to false" do
- subject.call("0").should equal false
+ typecaster.call("0").should equal false
end
it "casts '1' to true" do
- subject.call("1").should equal true
+ typecaster.call("1").should equal true
end
it "casts '0.0' to false" do
- subject.call("0.0").should equal false
+ typecaster.call("0.0").should equal false
end
it "casts '0.1' to true" do
- subject.call("0.1").should equal true
+ typecaster.call("0.1").should equal true
end
it "casts '-1' to true" do
- subject.call("-1").should equal true
+ typecaster.call("-1").should equal true
end
it "casts '-0.0' to false" do
- subject.call("-0.0").should equal false
+ typecaster.call("-0.0").should equal false
end
it "casts '-0.1' to true" do
- subject.call("-0.1").should equal true
+ typecaster.call("-0.1").should equal true
end
end
end
View
22 spec/unit/active_attr/typecasting/date_time_typecaster_spec.rb
@@ -4,61 +4,63 @@
module ActiveAttr
module Typecasting
describe DateTimeTypecaster do
+ subject(:typecaster) { described_class.new }
+
describe "#call" do