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
# common use of mocks in a Rails app | |
it "updates the requested book" do | |
mock_book = mock(:book) | |
Book.should_receive(:find).with("37").and_return(mock_book) | |
mock_book.should_receive(:update_attributes).with({:these => 'params'}) | |
put :update, :id => "37", :book => {:these => 'params'} | |
end | |
# without mocks a more readable, less brittle test | |
it "updates the requested book" do |
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
# Checking an interaction with a mock | |
it "should notify bidders in a mocky way" do | |
bidder = mock( :bidder ) | |
bidder.should_receive( :bid_changed ).with( '10.00' ) | |
auctioneer = Auctioneer.new | |
auctioneer.add_bidder( bidder ) | |
auctioneer.accept_bid( '10.00' ) | |
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
Feature: A simple shopping basket | |
Scenario: adding items | |
* 1 x $15 screw-driver | |
* 2 x $12 lamp | |
* Total bill = $39 |
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
When /^(\d+) x \$(\d+)/ do |qty, price| | |
@basket ||= [] | |
@basket << (qty.to_i * price.to_i) | |
end | |
Then /^Total bill = \$(.+)$/ do |expected_total| | |
@basket.inject(0) {|memo, item| memo += item}.should eql(expected_total.to_i) | |
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
def counter(start=0, increment=1) | |
lambda do | |
puts start | |
start += increment | |
end | |
end | |
result = counter(10, 2) | |
result.call # 10 |
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
def counter(start=0, increment=1) | |
lambda do | |
initial = start | |
start += increment | |
initial | |
end | |
end | |
result = counter(10, 2) |
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 AppServerConfiguration | |
attr_accessor :port, :admin_password | |
end | |
class Configuration | |
attr_accessor :tail_logs, :max_connections, :admin_password | |
def initialize(block) | |
block.call(self) | |
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
JMSMockObjectFactory objectFactory = new JMSMockObjectFactory(); | |
JMSTestModule testModule = new JMSTestModule(objectFactory); | |
TopicConnectionFactory beTopicConnectionFactory = objectFactory.createMockTopicConnectionFactory(); | |
Topic beTopic = objectFactory.getDestinationManager().createTopic("sampleTopic"); | |
//Perform you test - Publish the message | |
//Create the expected Message by the subscriber | |
MockMessage mockmessage = new MockMessage(); | |
//set properties to mockmessage | |
//condition - similar to assertequals | |
testModule.verifyReceivedTopicMessageEquals("sampleTopic", 1, mockmessage); |
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 Analytics | |
ConnectionFailure = Class.new(StandardError) | |
def self.sync(account) | |
new(Analytics::Client.new(CC.config[:analytics_api_key])).sync(account) | |
end | |
def initialize(analytics_client) | |
@analytics_client = analytics_client | |
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
describe MailingList do | |
let(:notifications) { double('notifications') } | |
let(:user) { User.new } | |
it 'registers a new user' do | |
expect(notifications).to receive(:call).with(user, 'list_name') | |
mailing_list = MailingList.new('list_name', notifications) | |
mailing_list.add(user) | |
OlderNewer