Forked from damonjmurray/Rspec_subject_behaviour_question.rb
Last active
February 10, 2016 03:20
-
-
Save geowy/52525c1bcdff9a7c697e to your computer and use it in GitHub Desktop.
RSpec subject behaviour question
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
class Foo | |
def self.bar(first, second, third) | |
err_msg = '%s cannot be nil' | |
raise ArgumentError, err_msg % 'first' unless first | |
raise ArgumentError, err_msg % 'second' unless second | |
raise ArgumentError, err_msg % 'third' unless third | |
"#{first} #{second} #{third}" | |
end | |
end | |
describe Foo do | |
[ | |
{ first: nil, second: 2, third: 3 }, | |
{ first: 1, second: nil, third: 3 }, | |
{ first: 1, second: 2, third: nil } | |
].each_with_index do |args_hash, index| | |
context "with arguments #{args_hash}" do | |
subject { Foo.bar(*args_hash.values) } | |
it "raises an ArgumentError when #{args_hash.keys[index]} is nil" do | |
expected_err_msg = "#{args_hash.keys[index]} cannot be nil" | |
expect { subject }.to raise_error(ArgumentError, expected_err_msg) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome - thanks @geowy!