Last active
October 28, 2015 18:30
-
-
Save kalarani/38730aba30049d1fd667 to your computer and use it in GitHub Desktop.
Use of Mocks in Unit Tests
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
| File.write(‘sample_file.txt’, reversed_string) |
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
| expect(File).to receive(:write).with('sample_file.txt', expected_string) |
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 Post | |
| def submit | |
| begin | |
| EmailService.send_mail @title, @author | |
| rescue | |
| Admin.notify | |
| end | |
| end | |
| end |
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
| expect(EmailService).to receive(:send_mail).with('New post', 'someone') |
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
| require './post.rb' | |
| require './email_service.rb' | |
| describe Post do | |
| describe '#submit' do | |
| it 'should send mail' do | |
| post = Post.new({:title => 'New post', :author => 'someone'}) | |
| expect(EmailService).to receive(:send_mail).with('New post', 'someone') | |
| post.submit | |
| end | |
| end | |
| end |
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
| require './post.rb' | |
| require './email_service.rb' | |
| require './admin.rb' | |
| describe Post do | |
| describe '#submit' do | |
| it 'should notify admin if sending email fails' do | |
| post = Post.new({:title => 'New post', :author => 'someone'}) | |
| expect(EmailService).to receive(:send_mail).and_throw('Something went wrong!') | |
| expect(Admin).to receive(:notify) | |
| post.submit | |
| end | |
| end | |
| end |
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
| require './post.rb' | |
| require './email_service.rb' | |
| require './admin.rb' | |
| describe Post do | |
| describe '#submit' do | |
| it 'should send mail' do | |
| post = Post.new({:title => 'New post', :author => 'someone'}) | |
| expect(EmailService).to receive(:send_mail).with('New post', 'someone') | |
| expect(Admin).to_not receive(:notify) | |
| post.submit | |
| end | |
| end | |
| end |
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 StringUtil | |
| def reverse a_string | |
| reversed_string = a_string.reverse | |
| File.write(‘sample_file.txt’, reversed_string) | |
| reversed_string | |
| end | |
| end |
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
| require './string_util' | |
| describe StringUtil do | |
| it 'should reverse a string' do | |
| string_util = StringUtil.new | |
| expected_string = 'tset a si sihT' | |
| expect(File).to receive(:write).with('sample_file.txt', expected_string) | |
| reversed_string = string_util.reverse('This is a test') | |
| expect(reversed_string).to eq expected_string | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment