Skip to content

Instantly share code, notes, and snippets.

@bgswan
bgswan / mock_problems.rb
Created December 31, 2010 15:52
Mock style test versus non mock test
# 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
@bgswan
bgswan / mock_versus_spy.rb
Created December 31, 2010 16:06
Example of a mock test and the same test with a spy
# 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
@bgswan
bgswan / cart.feature
Created February 24, 2011 17:34
A cucumber feature without the noise
Feature: A simple shopping basket
Scenario: adding items
* 1 x $15 screw-driver
* 2 x $12 lamp
* Total bill = $39
@bgswan
bgswan / cart_steps.rb
Created February 24, 2011 17:35
The step definitions for the cart feature
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
def counter(start=0, increment=1)
lambda do
puts start
start += increment
end
end
result = counter(10, 2)
result.call # 10
def counter(start=0, increment=1)
lambda do
initial = start
start += increment
initial
end
end
result = counter(10, 2)
@bgswan
bgswan / config.rb
Created April 7, 2011 10:44
Scottish Ruby Conference tutorial exercise
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
@bgswan
bgswan / mock_abuse.java
Created August 21, 2012 19:42
Pathalogical Mock Example
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);
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
@bgswan
bgswan / mailing_list_spec.rb
Last active December 24, 2015 15:19
Alternative version of test described here http://re-factor.com/blog/2013/09/27/slow-tests-are-the-symptom-not-the-cause/ using domain model rather than "service object"
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)