Created
May 6, 2015 18:12
-
-
Save danajp/3795eb79d573e4eb2f6d to your computer and use it in GitHub Desktop.
Using nested context blocks in rspec
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SpreadsheetValidator | |
def initialize(spreadsheet_reader) | |
@reader = spreadsheet_reader | |
@errors = [] | |
end | |
def has_errors? | |
@errors.length > 0 | |
end | |
def validate | |
@reader.rows.each do |row| | |
if !['Hired', 'Rejected'].include?(row[:status]) | |
@errors << "Invalid Status '#{row[:status]}'" | |
end | |
end | |
end | |
end | |
describe SpreadsheetValidator do | |
let(:reader) { double } | |
let(:rows) { [] } | |
let(:valid_row) do | |
{ | |
:id => 42, | |
:first_name => 'George', | |
:last_name => 'Costanza', | |
:company => 'Vandelay Industries', | |
:title => 'Latex Salesman', | |
:status => 'Hired' | |
} | |
end | |
subject { SpreadsheetValidator.new(reader) } | |
before do | |
allow(reader).to receive(:rows).and_return(rows) | |
end | |
describe 'validate' do | |
context 'with valid rows' do | |
let(:rows) { [ valid_row ] } | |
it 'should not have errors' do | |
subject.validate | |
expect(subject.has_errors?).to eq(false) | |
end | |
end | |
context 'with invalid status' do | |
let(:rows) { [ valid_row.merge(:status => 'some bullshit status') ] } | |
it 'should have errors' do | |
subject.validate | |
expect(subject.has_errors?).to eq(true) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment