Skip to content

Instantly share code, notes, and snippets.

@andyl
Created February 9, 2011 17:06
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save andyl/818821 to your computer and use it in GitHub Desktop.
Rspec / SinatraExtension: Solution Suggested by namelessjon
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