Skip to content

Instantly share code, notes, and snippets.

@backus
Created November 18, 2015 03:24
Show Gist options
  • Save backus/254d29794f1efad11c13 to your computer and use it in GitHub Desktop.
Save backus/254d29794f1efad11c13 to your computer and use it in GitHub Desktop.
Shoulda matchers `allow_value` bug
require 'active_model'
class Example
include ActiveModel::Model
validates :foo, inclusion: [1]
attr_accessor :foo
end
require 'date'
require_relative 'example'
require 'shoulda/matchers'
RSpec.describe Example do
include Shoulda::Matchers::ActiveModel
subject { described_class.new }
it { should allow_values(1).for(:foo) } # this should pass and does
it { should allow_values(2).for(:foo) } # this should fail and does
it { should allow_values(2, 1).for(:foo) } # this should fail and does
it { should allow_values(1, 2).for(:foo) } # this should fail and doesn't
end
Example
should allow foo to be set to 1
should allow foo to be set to 2 (FAILED - 1)
should allow foo to be set to any of [2, 1] (FAILED - 2)
should allow foo to be set to any of [1, 2]
Failures:
1) Example should allow foo to be set to 2
Failure/Error: it { should allow_values(2).for(:foo) } # this should fail and does
Did not expect errors when foo is set to 2,
got errors:
* "is not included in the list" (attribute: foo, value: 2)
# ./example_spec.rb:11:in `block (2 levels) in <top (required)>'
2) Example should allow foo to be set to any of [2, 1]
Failure/Error: it { should allow_values(2, 1).for(:foo) } # this should fail and does
Did not expect errors when foo is set to 2,
got errors:
* "is not included in the list" (attribute: foo, value: 2)
# ./example_spec.rb:12:in `block (2 levels) in <top (required)>'
Finished in 0.40018 seconds (files took 1.86 seconds to load)
4 examples, 2 failures
Failed examples:
rspec ./example_spec.rb:11 # Example should allow foo to be set to 2
rspec ./example_spec.rb:12 # Example should allow foo to be set to any of [2, 1]
rspec example_spec.rb --format documentation > output.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment