Created
February 9, 2011 17:06
-
-
Save andyl/818821 to your computer and use it in GitHub Desktop.
Rspec / SinatraExtension: Solution Suggested by namelessjon
This file contains 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 is working | |
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 is working now - see Jon's suggestion at | |
https://groups.google.com/d/topic/sinatrarb/6S2mZJq-QC4/discussion | |
Now the App code is re-defined in every spec, it sees the Constant | |
definition, and it includes the correct helper module. | |
=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 | |
AppDefinition = <<-EOF | |
class App < Sinatra::Base | |
register Sinatra::TestExtension | |
end | |
EOF | |
describe "App" do | |
it "should create a valid object" do | |
eval AppDefinition | |
obj = App.new | |
obj.sayname.should == "Helper2" | |
end | |
it "should recognize the CONSTANT set in the Spec" do | |
UseOne = true | |
eval AppDefinition | |
obj = App.new | |
obj.sayname.should == "Helper1" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment