ruby - undefined local variable or method error when trying to use `instance_variable_set` in rspec -
i have class this
require 'net/http' class foo def initialize @error_count = 0 end def run result = net::http.start("google.com") @error_count = 0 if result rescue @error_count += 1 end end and spec file it.
require_relative 'foo'
describe foo let(:foo){ foo.new} describe "#run" context "when fails 30 times" foo.instance_variable_set(:@error_count, 30) end end end and run rspec foo_spec.rb, fails error.
foo_spec.rb:7:in `block (3 levels) in <top (required)>': undefined local variable or method `foo' #<class:0x007fc37410c400> (nameerror) how should call instance_variable_set method in rspec?
edit
i want call send_error method if 30 times fails.
require 'net/http' class foo def initialize @error_count = 0 end def run result = net::http.start("google.com") @error_count = 0 if result rescue @error_count += 1 send_error if @error_count >= 30 end def send_error end end and spec file test send_error called when connection fails 30 times.
require_relative 'foo' describe foo let(:foo){ foo.new} describe "#run" context "when fails 30 times" "should send error" foo.instance_variable_set(:@error_count, 30) expect(foo).to receive(:send_error) end end end end
i don't know you're trying do, suspect it's not right approach things.
however, immediate problem you're not in context of test, foo undefined. want wrap foo.instance_variable_set in test construct - either it or specify block, or before :each, or similar.
Comments
Post a Comment