Question

I'm trying to write a custom parser for my cucumber results. In doing so, I want to write rspec tests around it. What I currently have is as follows:

describe 'determine_test_results' do
  it 'returns a scenario name as the key of the scenario results, with the scenario_line attached' do
    pcr = ParseCucumberJsonReport.new
    expected_results = {"I can login successfully"=>{"status"=>"passed", "scenario_line"=>4}}
    cucumber_results = JSON.parse(IO.read('example_json_reports/json_passing.json'))
    pcr.determine_test_results(cucumber_results[0]).should == expected_results
  end
end

The problem is, determine_test_results has a sub method called determine_step_results, which means this is really an integration test between the 2 methods and not a unit test for determine_test_results.

How would I mock out the "response" from determine_step_results?

Assume determine_step_results returns {"status"=>"passed", "scenario_line"=>4}

what I have tried:

pcr.stub(:determine_step_results).and_return({"status"=>"passed", "scenario_line"=>6})

and

allow(pcr).to receive(:determine_step_results).and_return({"status"=>"passed", "scenario_line"=>6})
Was it helpful?

Solution

You could utilize stubs for what you're trying to accomplish. Project: RSpec Mocks 2.3 would be good reading regarding this particular case. I have added some code below as a suggestion.

describe 'determine_test_results' do
 it 'returns a scenario name as the key of the scenario results, with the scenario_line       attached' do
   pcr = ParseCucumberJsonReport.new
   expected_results = {"I can login successfully"=>{"status"=>"passed", "scenario_line"=>4}}

   # calls on pcr will return expected results every time determine_step_results is called in any method on your pcr object.
   pcr.stub!(:determine_step_results).and_return(expected_results)

   cucumber_results = JSON.parse(IO.read('example_json_reports/json_passing.json'))
   pcr.determine_test_results(cucumber_results[0]).should == expected_results
 end
end

OTHER TIPS

If all what determine_test_results does is call determine_step_results, you should not really test it, since it is trivial...

If you do decide to test it, all you need to test is that it calls the delegate function, and returns whatever is passed to it:

describe ParseCucumberJsonReport do
  describe '#determine_test_results' do
    it 'calls determine_step_results' do
      result = double(:result)
      input = double(:input)
      expect(subject).to receive(:determine_step_results).with(input).and_return(result)

      subject.determine_test_results(input).should == result
    end
  end
end

If it is doing anything more (like adding the result to a larger hash) you can describe it too:

describe ParseCucumberJsonReport do
  describe '#determine_test_results' do
    it 'calls determine_step_results' do
      result = double(:result)
      input = double(:input)
      expect(subject).to receive(:determine_step_results).with(input).and_return(result)
      expect(subject.larger_hash).to receive(:merge).with(result)

      subject.determine_test_results(input).should == result
    end
  end
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top