So, I think there is probably an easier way to verify that you are trying to test, but without additional context, I donβt know what to recommend. However, here is the code for the evidence-based concept to show what you can do:
describe "test" do class TestClass attr_accessor :opts def initialize(opts={}) @opts = opts end def bar [] end end let!(:stubbed) do TestClass.new(args).tap{|obj| obj.stub(:bar).and_return("bar")} end let!(:unstubbed) { TestClass.new(args) } before :each do TestClass.stub(:new) do |args| case args when { :foo => "foo" } stubbed else unstubbed end end end subject { TestClass.new(args) } context "special arguments" do let(:args) { { :foo => "foo" } } its(:bar) { should eq "bar" } its(:opts) { should eq({ :foo => "foo" }) } end context "no special arguments" do let(:args) { { :baz => "baz" } } its(:bar) { should eq [] } its(:opts) { should eq({ :baz => "baz" }) } end end
test special arguments bar should == bar opts should == {:foo=>"foo"} no special arguments bar should == [] opts should == {:baz=>"baz"} Finished in 0.01117 seconds 4 examples, 0 failures
However, I very often use special themes / context blocks. See http://benscheirman.com/2011/05/dry-up-your-rspec-files-with-subject-let-blocks/ for more details.
source share