Created
March 15, 2011 04:02
-
-
Save mipearson/870288 to your computer and use it in GitHub Desktop.
.. an example of mocking gone too far.
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
## 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