Steve
Steve

Reputation: 2854

How to test variable assignment in RSpec

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.

Upvotes: 0

Views: 961

Answers (1)

Mori
Mori

Reputation: 27779

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

Upvotes: 2

Related Questions