Created
February 9, 2011 15:08
-
-
Save andyl/818611 to your computer and use it in GitHub Desktop.
Example Spec: Using Constants and Sinatra Extensions
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 'spec_helper' | |
=begin | |
Example One - this works | |
In this example, the constant (ZZZ) is set in the | |
Spec, and detected in the target code. | |
=end | |
module TestModule | |
class TestClass | |
def method1 | |
ZZZ | |
end | |
end | |
end | |
describe TestModule::TestClass do | |
it "should recognize the CONSTANT" do | |
ZZZ = "HELLO WORLD" unless defined?(ZZZ) | |
obj = TestModule::TestClass.new | |
obj.method1.should == ZZZ | |
end | |
end | |
require 'sinatra/base' | |
=begin | |
Example Two - this doesn't work | |
In this example, the constant (UseOne) is set in the | |
Spec, and isn't detected in the target code. | |
The question is: how to make the target code detect | |
the constant. | |
=end | |
module Sinatra | |
module TestExtension | |
module Helper1 | |
def sayname() "Helper1"; end | |
end | |
module Helper2 | |
def sayname() "Helper2"; end | |
end | |
def self.registered(app) | |
app.helpers defined?(UseOne) ? Helper1 : Helper2 | |
end | |
end | |
end | |
class App < Sinatra::Base | |
register Sinatra::TestExtension | |
end | |
describe App do | |
it "should create a valid object" do | |
obj = App.new | |
obj.sayname.should == "Helper2" | |
end | |
it "should recognize the CONSTANT set in the Spec" do | |
obj = App.new | |
UseOne = true | |
obj.sayname.should == "Helper1" # <<< this fails | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment