Reputation: 449
Quick rspec question here.
Given the following test...
it 'sets the correct params' do
property = 'blahblah'
expect(ConnectionClass).to receive(:new).with({ opt1: param1, opt2: property })
subject.send(:do_things, nil, nil).and_return_nil
end
and the following method...
private
def do_things(param1, param2)
connection = ConnectionClass.new(opt1: param1, opt2: param2)
##
# How do i stop the test from continue beyond this point? :(
##
some_var = connection.build_request do |blah|
# ...
end
some_var.some_attribute
end
Running the test results in the following failure:
Failures:
1) Klass#do_things sets the correct params
Failure/Error: subject.send(:do_things, nil, nil).and_return_nil
NoMethodError:
undefined method `build_request' for nil:NilClass
# ./lib/blah/Klass.rb:46:in `make_request'
# ./spec/lib/blah/Klass_spec.rb:79:in `block (3 levels) in <top (required)>'
For this test, all i care about is that ConnectionClass
is initialized correctly -- how can i prevent the call to build_request
and eventual some_attribute
?
Thank you
Upvotes: 0
Views: 1173
Reputation: 62648
This is generally a pretty good signal that you either need to refactor your code, or you are testing things that you need to not be testing. Rather than testing the implementation ("This class is instantiated with these parameters") consider testing your outputs (For input X, do_things
should return output Y and have side effects Z). If you think "I want this test to stop in the middle of this method", you need to refactor the method you're testing so that you can discretely test just the bits you want to test.
That said, if you don't want to change your approach here, you could just return a double from your stub, so that it can complete the method.
some_var = double(some_attribute: "value")
connection = double(build_request: some_var)
expect(ConnectionClass).to receive(:new).with({ opt1: param1, opt2: property }).
and_return(connection)
Upvotes: 3