Skip to content

Instantly share code, notes, and snippets.

@mipearson
Created March 15, 2011 04:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mipearson/870288 to your computer and use it in GitHub Desktop.
Save mipearson/870288 to your computer and use it in GitHub Desktop.
.. an example of mocking gone too far.
## configuration_spec.rb
require 'spec_helper'
describe Configuration do
context "with the configuration variable set " do
before do
@config = Configuration.new(:key => 'hello', :value => 'goodbye')
mock(Configuration).find_by_key('hello') { @config }
end
describe ".get" do
it "should retrieve a configuration value" do
Configuration.get('hello').should == 'goodbye'
end
end
describe ".set" do
it "should overwrite the old value" do
mock(@config).save
Configuration.set('hello', 'foo')
@config.value.should == 'foo'
end
end
end
context "without the configuration variable set " do
before do
mock(Configuration).find_by_key('hello') { nil }
end
describe ".get" do
it "should return nil" do
Configuration.get('hello').should be nil
end
end
describe ".set" do
it "should create a new entry" do
config = Configuration.new
mock(config).save
mock(Configuration).new(:key => 'hello') { config }
Configuration.set('hello', 'foo')
config.value.should == 'foo'
end
end
end
end
## configuration.rb
class Configuration < ActiveRecord::Base
def self.get key
conf = find_by_key(key)
conf ? conf.value : nil
end
def self.set key, val
conf = find_by_key(key) || new(:key => key)
conf.value = val
conf.save
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment