Re: Stubbing helper methods with RSpec

305 views
Skip to first unread message

Dan Brooking

unread,
Mar 16, 2013, 10:16:01 AM3/16/13
to sina...@googlegroups.com
I was able to figure this out.  I needed to add Sinatra::Application to my random_string call.  This then gave me a error on calling a private method.  So I wrapped the call in an instance_eval and that did the trick.

I broke the compare (told it to expect "abcdefg") just to confirm it was working and it was.


On Sat, Mar 16, 2013 at 8:51 AM, Dan Brooking <dmbro...@gmail.com> wrote:
I've been searching and found quite a few with this question but no real answers.  I have a helper method that returns a random string...

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)
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?

--
You received this message because you are subscribed to the Google Groups "sinatrarb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sinatrarb+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Piotr Szotkowski

unread,
Mar 16, 2013, 12:47:23 PM3/16/13
to sina...@googlegroups.com
Dan Brooking:

> I have a helper method that returns a random string...

> 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)
> str.should == "abcdef"
> end
> end

I see that you’ve solved your problem already, but please
note that the above does not test the method at all; you
can remove the method’s body and it would pass just as well.
There’s also a bug in the implementation, as the returned
string is one character longer than the passed length.

If you do want to test the method, you can seed the
random number generator with known values (remembering
to re-seed it for other tests) and check the results:

require 'minitest/autorun'

def random_string length
(0...length).map{ rand(36).to_s 36 }.join
end

describe '#random_string' do
it 'returns a random string (0-9a-z) of the given length' do
begin
srand 0
random_string(6).must_equal '0339jl'
srand 1
random_string(6).must_equal 'c89b5f'
random_string(7).must_equal '0g1c76p'
ensure
srand
end
end
end

Also, if you want to test the behaviour of other
random-based methods, note that (at least in Ruby
2.0) most of them can take a randomness source:

https://github.com/chastell/signore/blob/8991977221d61fd43ac341ab0e8429df4858f109/lib/signore/database.rb#L13-20
https://github.com/chastell/signore/blob/8991977221d61fd43ac341ab0e8429df4858f109/spec/signore/database_spec.rb#L8-13

(I’m not saying this is the best way to test
this, but it does work for me quite well.)

— Piotr Szotkowski
--
Yo dog, I heard you like bugs, so we put a bug in your debugger,
so that while you debug your bug, you experience another bug.
[Aman Gupta]



signature.asc

Dan Brooking

unread,
Mar 25, 2013, 7:02:03 PM3/25/13
to sina...@googlegroups.com
Thanks.. yes I am aware of that.  :)

I'm fairly new to testing and stubbing methods and this is a method that is used pretty extensively in one part of my app... so I wanted to basically test that I was stubbing properly more than testing the actual method.

The code you posted however will be very helpful for future use because I have run into times when I wanted to actual test the random code and wasn't quite sure how to do it.

Thanks!

Carlos Eduardo L. Lopes

unread,
Mar 26, 2013, 9:37:38 AM3/26/13
to sina...@googlegroups.com
I would do this using another approach.. and i think it's easier than Piotr way:

describe "#random_string" do
�� it "returns a random string" do
���� Kernel.stub!(:rand).with(36).and_return((10..15).to_a)
�� � random_string(6).must_equal('abcdef')
�� end
end


ps: i didn't test this, but i think it will work :)
--
Carlos Eduardo L. Lopes
Skype: carloslopespollares | Twitter: http://twitter.com/_carloslopes
Github: http://github.com/carloslopes


March 25, 2013 8:02 PM
Thanks.. yes I am aware of that. �:)

I'm fairly new to testing and stubbing methods and this is a method that is used pretty extensively in one part of my app... so I wanted to basically test that I was stubbing properly more than testing the actual method.

The code you posted however will be very helpful for future use because I have run into times when I wanted to actual test the random code and wasn't quite sure how to do it.

Thanks!
--
You received this message because you are subscribed to the Google Groups "sinatrarb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sinatrarb+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
�
�
March 16, 2013 9:51 AM
I've been searching and found quite a few with this question but no real answers. �I have a helper method that returns a random string...

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)
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?
--
You received this message because you are subscribed to the Google Groups "sinatrarb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sinatrarb+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
�
�

Piotr Szotkowski

unread,
Mar 27, 2013, 3:37:28 AM3/27/13
to sina...@googlegroups.com
Carlos Eduardo L. Lopes:

> I would do this using another approach..
> and i think it's easier than Piotr way:

> describe "#random_string" do
> it "returns a random string" do
> Kernel.stub!(:rand).with(36).and_return((10..15).to_a)
> random_string(6).must_equal('abcdef')
> end
> end

Ah, good approach. :)

You could also pass a callable to random_string that defaults to
&method(:rand) and inject a known-value generating one in tests. ;]

— Piotr Szotkowski
--
You’ve already spent more time optimizing this code
than the server will spend executing it in a year.
[David Brady]



signature.asc

Carlos Eduardo L. Lopes

unread,
Mar 27, 2013, 9:19:22 AM3/27/13
to sina...@googlegroups.com
Sure, this is an option too! :)

--
Carlos Eduardo L. Lopes
Skype: carloslopespollares | Twitter: http://twitter.com/_carloslopes
Github: http://github.com/carloslopes


March 27, 2013 4:37 AM
Carlos Eduardo L. Lopes:

I would do this using another approach..
and i think it's easier than Piotr way:
describe "#random_string" do
   it "returns a random string" do
     Kernel.stub!(:rand).with(36).and_return((10..15).to_a)
     random_string(6).must_equal('abcdef')
   end
end
Ah, good approach. :)

You could also pass a callable to random_string that defaults to
&method(:rand) and inject a known-value generating one in tests. ;]

— Piotr Szotkowski
March 26, 2013 10:37 AM
I would do this using another approach.. and i think it's easier than Piotr way:

describe "#random_string" do
   it "returns a random string" do
     Kernel.stub!(:rand).with(36).and_return((10..15).to_a)
     random_string(6).must_equal('abcdef')
   end
end


ps: i didn't test this, but i think it will work :)


March 16, 2013 9:51 AM
I've been searching and found quite a few with this question but no real answers.  I have a helper method that returns a random string...

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)
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 ==)
Reply all
Reply to author
Forward
0 new messages