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) }.joinendI have my test written as follows:describe "#random_string" doit "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"endendI 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 stringFailure/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.
March 25, 2013 8:02 PMThanks.. 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 AMI'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) }.joinend
I have my test written as follows:
describe "#random_string" doit "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"endend
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.
�
�
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 endAh, 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) }.joinend
I have my test written as follows:
describe "#random_string" doit "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"endend
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 ==)