Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geowy/52525c1bcdff9a7c697e to your computer and use it in GitHub Desktop.
Save geowy/52525c1bcdff9a7c697e to your computer and use it in GitHub Desktop.
RSpec subject behaviour question
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
@damonjmurray
Copy link

Awesome - thanks @geowy!

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