Skip to content

Instantly share code, notes, and snippets.

@andyl
Created March 21, 2011 04:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andyl/879029 to your computer and use it in GitHub Desktop.
Save andyl/879029 to your computer and use it in GitHub Desktop.
# test2_spec.rb - call using 'rspec test2_spec.rb'
require 'rspec'
require 'mocha'
RSpec::configure do |config|
config.mock_with :mocha
end
class Test2
attr_accessor :msg
def initialize
@msg = sayhi
end
def sayhi
"hi"
end
end
describe Test2 do
describe Test2, "stubbing methods used during object construction" do
it "should assign the stub value to the instance variable" do
Test2.any_instance.stubs(:sayhi).returns("first")
Test2.new.msg.should == "first"
end
end
describe Test2, "stubbing methods after the object is created" do
it "should redefine the return value of a stubbed method" do
obj = Test2.new
obj.stubs(:sayhi).returns("second")
obj.sayhi.should == "second"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment