I got it...
I called it using instance_eval.
def app
Sinatra::Application
end
describe "#random_string" do
it "returns a random string" do
app.should_receive(:random_string).with(6).and_return('abcdef')
str = app.instance_eval("random_string(6)")
str.should == "abcdef"
end
end
On Sat, Mar 16, 2013 at 10:05 AM, Dan Brooking <dmbrooking at gmail.com> wrote:
> OK, so I think based on this, I'm doing it right and it's a Sinatra issue.
> I did as you suggested and made a call to
> Sinatra::Application.random_string and now my error is "private method
> `random_string' called for Sinatra::Application:Class".
>> So, like I said, looks to me like a Sintra specific issue where I need to
> reference it in a certain way. It's such a small method, I don't really
> need to test this but it's used in other modules so I wanted to isolate
> this to ensure I've got the stubbing down right.
>> Thanks!
>>> On Sat, Mar 16, 2013 at 9:58 AM, David Chelimsky <dchelimsky at gmail.com>wrote:
>>> 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.should_receive(:random_string).with(6).and_return('abcdef')
>> Sinatra::Application.random_string(6).should eq 'abcdef'
>>>> HTH,
>> David
>>>> > 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>> _______________________________________________
>> rspec-users mailing list
>>rspec-users at rubyforge.org>>http://rubyforge.org/mailman/listinfo/rspec-users>>>>-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20130316/7e652024/attachment.html>