Skip to content
This repository

Add new active model matcher `use_default_value(value = nil).for(:attribute)` #47

Closed
wants to merge 1 commit into from

3 participants

Gabe Berke-Williams Mike Burns

Add new active model matcher use_default_value(value = nil).for(:attribute). This mathcher ensures that the attribute’s default value is set correctly.

Ensures that the attribute's default value is set correctly.

Examples:

it { should_not use_default_value(0).for(:count) } #the :count field's default value is not 0
it { should_not use_default_value.for(:count) } #the :count field has no default value (e.g. it's default value is nil)
it { should use_default_value(0).for(:count) } #the :count field's default value is O
it { should use_default_value.for(:count) } #the :count field has a default value (e.g. it's default value is not nil)

Dorian LUPU Add new active model matcher use_default_value(value = nil).for(:attr…
…ibute)

Ensures that the attribute's default value is set correctly.

 Examples:

 it { should_not use_default_value(0).for(:count) }  #the :count field's default value is not 0
 it { should_not use_default_value.for(:count) }     #the :count field has no default value (e.g. it's default value is nil)
 it { should use_default_value(0).for(:count) }      #the :count field's default value is O
  it { should use_default_value.for(:count) }         #the :count field has a default value (e.g. it's default value is not nil)
fd40bee
Mike Burns mike-burns commented on the diff
...atchers/active_model/use_default_value_for_matcher.rb
... ... @@ -0,0 +1,64 @@
  1 +module Shoulda # :nodoc:
  2 + module Matchers
  3 + module ActiveModel # :nodoc:
  4 +
  5 + # Ensures that the attribute's default value is set correctly.
  6 + #
  7 + # Example:
  8 + # it { should_not use_default_value(0).for(:count) } #the :count field's default value is not 0
  9 + # it { should_not use_default_value.for(:count) } #the :count field has no default value (e.g. it's default value is nil)
  10 + # it { should use_default_value(0).for(:count) } #the :count field's default value is O
  11 + # it { should use_default_value.for(:count) } #the :count field has a default value (e.g. it's default value is not nil)
3
Mike Burns Owner
mike-burns added a note

I'd like to remove the comment explaining what the matcher means. What can we rename this matcher such that this is possible?

it { should have_a_default_of(0).for(:count) }
it { should default_to(0).on_the(:count).column }
its(:count) { should default_to(0) }

?

Gabe Berke-Williams Collaborator
gabebw added a note

@dlupu - any thoughts on Mike's comment above? I think that renaming it such that its usage is immediately obvious is a good idea.

dlupu added a note

I agree with @mike-burns . I'll update this very soon and commit again. Thank you for your feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
dlupu referenced this pull request from a commit in dlupu/shoulda-matchers
dlupu #47 Ensure that the attribute's default value is set correctly. Examp…
…les:

it { should have_a_default.for(:count) }
it { should have_a_default.of(0).for(:count) }
it { should default_to(0).on_the(:count).column }

For more information, read the comments :)
6ae941b
dlupu commented

@mike-burns @gabebw I've made a new commit (dlupu@6ae941b) that is closer to your suggestions. Should I close this topic and make a new pull request?

Gabe Berke-Williams
Collaborator

@dlupu: Yes, that looks excellent. I'm going to close this pull request - please create another one with those changes.

Thanks!

-- Gabe

Gabe Berke-Williams gabebw closed this
Gabe Berke-Williams
Collaborator

@dlupu - any progress on this?

-- Gabe

dlupu commented

Done. Sorry for the delay, I'm very busy these days. Anyway, I'm happy to contribute to this gem that I'm been using for some while now :)

Gabe Berke-Williams
Collaborator

Hey, no worries - thanks for the code! I'll take a look tomorrow.

-- Gabe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Jan 25, 2012
Dorian LUPU Add new active model matcher use_default_value(value = nil).for(:attr…
…ibute)

Ensures that the attribute's default value is set correctly.

 Examples:

 it { should_not use_default_value(0).for(:count) }  #the :count field's default value is not 0
 it { should_not use_default_value.for(:count) }     #the :count field has no default value (e.g. it's default value is nil)
 it { should use_default_value(0).for(:count) }      #the :count field's default value is O
  it { should use_default_value.for(:count) }         #the :count field has a default value (e.g. it's default value is not nil)
fd40bee
This page is out of date. Refresh to see the latest.
2  lib/shoulda/matchers/active_model.rb
@@ -10,7 +10,7 @@
10 10 require 'shoulda/matchers/active_model/validate_acceptance_of_matcher'
11 11 require 'shoulda/matchers/active_model/validate_numericality_of_matcher'
12 12 require 'shoulda/matchers/active_model/allow_mass_assignment_of_matcher'
13   -
  13 +require 'shoulda/matchers/active_model/use_default_value_for_matcher'
14 14
15 15 module Shoulda
16 16 module Matchers
64 lib/shoulda/matchers/active_model/use_default_value_for_matcher.rb
... ... @@ -0,0 +1,64 @@
  1 +module Shoulda # :nodoc:
  2 + module Matchers
  3 + module ActiveModel # :nodoc:
  4 +
  5 + # Ensures that the attribute's default value is set correctly.
  6 + #
  7 + # Example:
  8 + # it { should_not use_default_value(0).for(:count) } #the :count field's default value is not 0
  9 + # it { should_not use_default_value.for(:count) } #the :count field has no default value (e.g. it's default value is nil)
  10 + # it { should use_default_value(0).for(:count) } #the :count field's default value is O
  11 + # it { should use_default_value.for(:count) } #the :count field has a default value (e.g. it's default value is not nil)
  12 + #
  13 + def use_default_value(value=nil)
  14 + UseDefaultValueMatcher.new(value)
  15 + end
  16 +
  17 + class UseDefaultValueMatcher # :nodoc:
  18 +
  19 + def initialize(value)
  20 + @default_value = value
  21 + end
  22 +
  23 + def for(attribute)
  24 + @attribute = attribute
  25 + self
  26 + end
  27 +
  28 + def matches?(instance)
  29 + #instance.send("#{@attribute}=".to_sym) = nil
  30 + @instance = instance
  31 + @instance.valid?
  32 + @default_value.nil? ? value : value == @default_value
  33 + end
  34 +
  35 + def failure_message
  36 + if @default_value
  37 + "Expected default value for #{@attribute} to be #{@default_value.inspect} but it was #{value.inspect} !"
  38 + else
  39 + "Expected to find a default value for #{@attribute} but there wasn't any !"
  40 + end
  41 + end
  42 +
  43 + def negative_failure_message
  44 + if @default_value
  45 + "Expected default value for #{@attribute} not to be #{@default_value.inspect} but it was!"
  46 + else
  47 + "Not expecting to find a default value for #{@attribute} but found #{value.inspect}!"
  48 + end
  49 + end
  50 +
  51 + def description
  52 + "checks if the #{@attribute}'s default value is #{@default_value.inspect}"
  53 + end
  54 +
  55 + private
  56 +
  57 + def value
  58 + @instance.send(@attribute.to_sym)
  59 + end
  60 + end
  61 +
  62 + end
  63 + end
  64 +end
42 spec/shoulda/active_model/use_default_value_for_matcher_spec.rb
... ... @@ -0,0 +1,42 @@
  1 +require 'spec_helper'
  2 +
  3 +describe Shoulda::Matchers::ActiveModel::UseDefaultValueMatcher do
  4 +
  5 + context "an attribute with a default value" do
  6 + before do
  7 + define_model :example, :attr => :string do
  8 + before_validation :set_attr
  9 + def set_attr
  10 + self.attr = "abc" if attr.nil?
  11 + end
  12 + end
  13 + @model = Example.new
  14 + end
  15 +
  16 + it "should use the right default value" do
  17 + @model.should use_default_value("abc").for(:attr)
  18 + end
  19 +
  20 + it "should use a default value" do
  21 + @model.should use_default_value.for(:attr)
  22 + end
  23 +
  24 + it "should not use a bad default value" do
  25 + @model.should_not use_default_value("xyz").for(:attr)
  26 + end
  27 + end
  28 +
  29 + context "an attribute without a default value" do
  30 + before do
  31 + define_model :example, :attr => :string
  32 + @model = Example.new
  33 + end
  34 + it "should not use a default value" do
  35 + @model.should_not use_default_value.for(:attr)
  36 + end
  37 +
  38 + it "should not use a bad default value" do
  39 + @model.should_not use_default_value("xyz").for(:attr)
  40 + end
  41 + end
  42 +end

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.