[rspec-users] stubbed methods are not returning what I've stubbed them to return

David Chelimsky dchelimsky at gmail.com
Sat Mar 16 13:58:57 UTC 2013

On Sat, Mar 16, 2013 at 7:56 AM, Dan Brooking <dmbrooking at gmail.com> wrote:
> I'm working on a small Sintra app and am having trouble getting my stub to
> return what I want it to.  I've been searching and found quite a few with
> this question but no real answers.  I also posted this to the sinatrarb
> google group in case it's an issue there.
> I have a helper method that returns a random string. I'm hoping it's
> something very simple I'm doing wrong
> def random_string(length)
>   (0..length).map{ rand(36).to_s(36) }.join
> end
> I have my test written as follows:
> describe "#random_string" do
> it "returns a random string" do
> #Sinatra::Application.any_instance.stub(:random_string).and_return("abcdef")
> Sinatra::Application.should_receive(:random_string).with(6).and_return('abcdef')
> str = random_string(6)

Here ^^ random_string is being called in the context of the example,
not the Sinatra::Application class or any of its instances. I don't
know how Sinatra includes helper modules, but assuming that includes
them in the Sinatra::Application object, then this should work:

Sinatra::Application.random_string(6).should eq 'abcdef'


> str.should == "abcdef"
> end
> end
> I have tried both lines shown in the code, and both times, the code runs.
> Yet my tests are still failing.  It doesn't look like the stub is taking. My
> failure looks like:
> #random_string returns a random string
>      Failure/Error: str.should == "abcdef"
>        expected: "abcdef"
>             got: "xcz0g7a" (using ==)
> Any ideas?
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

More information about the rspec-users mailing list