0
def hide_gridlines(sheet, hide_gridlines)
  sheet.sheet_view.show_grid_lines = false if hide_gridlines
 end

I need to test this method in RSpec / Ruby, and I want to write two tests, one to see if this code sheet.sheet_view.show_grid_lines = false executes if hide_gridlines is true.

The other is to ensure that code sheet.sheet_view.show_grid_lines = false does not run if hide_gridlines is false.

sheet is a worksheet that comes from the Axlsx Gem, and I created a double, but I'm not sure how to setup the double to have a sheet_view, and then test the variable declaration.

Any pointers on how to test this in RSpec, your help would be appreciated.

1
  • Are you able to refactor this code? You might find that testing comes easier when this method is simplified. Commented Nov 29, 2015 at 22:32

1 Answer 1

2
context 'set sheet.sheet_view.show_grid_lines to' do
  let(:sheet_view) { double }
  let(:sheet) { double(sheet_view: sheet_view) }

  specify 'false if hide_gridlines' do
    expect(sheet_view).to receive(:show_grid_lines=).with(false)
    subject.hide_gridlines(sheet, true)
  end

  specify 'true unless hide_gridlines' do
    expect(sheet_view).not_to receive(:show_grid_lines=)
    subject.hide_gridlines(sheet, false)
  end
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.