31 Jul Rspec test an example when you use an instance variable
My test file content:
describe ProductsService do describe '#valid?' do context 'when is successfully' do let(:result) { mock 'Result', object: {'errors' => ''}} before do subject.instance_variable_set(:@result, result) end specify do expect(subject.valid?).to be_true end end context 'when there is an error' do let(:result) { mock 'Result', object: {'errors' => 'error example'}} before do subject.instance_variable_set(:@result, result) end specify do expect(subject.valid?).to be_false end end end
My products file content:
class ProductsService def first_method params @result = dosomething params end def valid? @result.object['errors'].empty? end end
No Comments