Skip to content

Instantly share code, notes, and snippets.

@gong023
Created September 13, 2013 09:32
Show Gist options
  • Save gong023/6548506 to your computer and use it in GitHub Desktop.
Save gong023/6548506 to your computer and use it in GitHub Desktop.
rspec_stub_bug????
require 'yaml'
class A
def load_setting
YAML.load_file("./hoge.yaml") # hoge.yaml => setting: 1
end
end
class B
CONST = A.new.load_setting
CONST2 = YAML.load_file("./hoge.yaml")
def self.load_setting_singleton
YAML.load_file("./hoge.yaml")
end
def load_setting
YAML.load_file("./hoge.yaml")
end
end
describe A do
# success
it "should 1" do
expect = {"setting" => 1}
A.new.load_setting.should eq expect
end
end
describe B do
before do
setting_stub = {"setting" => 2}
YAML.stub(:load_file).and_return setting_stub
end
# success
context "load_setting_singleton" do
it "shoud be 2" do
expected = {"setting" => 2}
B.load_setting_singleton.should eq expected
end
end
# success
context "load_setting" do
it "should be 2" do
expected = {"setting" => 2}
B.new.load_setting.should eq expected
end
end
# fail
# not applied stub
context "CONST" do
it "shoud be 2" do
expected = {"setting" => 2}
B::CONST.should eq expected
end
end
# fail
# not applied stub
context "CONST2" do
it "shoud be 2" do
expected = {"setting" => 2}
B::CONST2.should eq expected
end
end
end
@gong023
Copy link
Author

gong023 commented Sep 13, 2013

stub_const

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment