Skip to content

Instantly share code, notes, and snippets.

@andyl
Created Feb 9, 2011
Embed
What would you like to do?
Example Spec: Using Constants and Sinatra Extensions
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