Jferg
2010-May-07 01:03 UTC
[rspec-users] RSpec newb - why is my negative test not passing?
Why won''t this test work? I am trying to use RSpec to drive a negative test where my code will throw an exception which it does however, the test does not pass. It fails due to my exception. However, I am using should raise_exception: ----------------------- describe "Making non-existent url tiny" do it "should raise exception" do url = "tttttt.dddddd" exception_new = Exception.new "Test URL Error" TinyUrlService.should_receive(:get).with(url).and_raise(exception_new) TinyUrlService.make_tiny(url).should raise_exception end end ========================================== Here is the code under test: ------------------------------------- require ''httparty'' class TinyUrlService class InvalidUrl < Exception def intialize original_e super original_e.message end end include HTTParty base_uri ''tinyurl.com'' def self.make_tiny (targetUrl) return get(''/api-create.php'', :query =>{:url => validate_url(targetUrl)}) end private def self.validate_url(tiny_url) get(tiny_url) return tiny_url rescue Exception => e raise InvalidUrl.new(e) end end ====================================Here is the exception reported: TinyUrlService::InvalidUrl: Test URL Error ../lib/tiny_url_service.rb:23:in `validate_url'' ../lib/tiny_url_service.rb:15:in `make_tiny'' /Users/johnferguson/ruby/flitter/specs/tiny_url_spec.rb:11: -e:1:in `load'' -e:1:
Rick DeNatale
2010-May-08 16:43 UTC
[rspec-users] RSpec newb - why is my negative test not passing?
On Thu, May 6, 2010 at 9:03 PM, Jferg <johngferguson at gmail.com> wrote:> Why won''t this test work? > > I am trying to use RSpec to drive a negative test where my code will > throw an exception which it does however, the test does not pass. ?It > fails due to my exception. ?However, I am using should > raise_exception: > ----------------------- > > describe "Making non-existent url tiny" do > > ?it "should raise exception" do > ? ?url = "tttttt.dddddd" > ? ?exception_new = Exception.new "Test URL Error" > > TinyUrlService.should_receive(:get).with(url).and_raise(exception_new) >? ?lambda {TinyUrlService.make_tiny(url)}.should raise_exception> ?end > end-- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Github: http://github.com/rubyredrick Twitter: @RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale
Jferg
2010-May-09 02:56 UTC
[rspec-users] RSpec newb - why is my negative test not passing?
Thanks, next time I''ll read the documentation with more attention to what is written there. It works as you said and as is documented. On May 8, 12:43?pm, Rick DeNatale <rick.denat... at gmail.com> wrote:> On Thu, May 6, 2010 at 9:03 PM, Jferg <johngfergu... at gmail.com> wrote: > > Why won''t this test work? > > > I am trying to use RSpec to drive a negative test where my code will > > throw an exception which it does however, the test does not pass. ?It > > fails due to my exception. ?However, I am using should > > raise_exception: > > ----------------------- > > > describe "Making non-existent url tiny" do > > > ?it "should raise exception" do > > ? ?url = "tttttt.dddddd" > > ? ?exception_new = Exception.new "Test URL Error" > > > TinyUrlService.should_receive(:get).with(url).and_raise(exception_new) > > ?? ?lambda {TinyUrlService.make_tiny(url)}.should raise_exception > > > ?end > > end > > -- > Rick DeNatale > > Blog:http://talklikeaduck.denhaven2.com/ > Github:http://github.com/rubyredrick > Twitter: @RickDeNatale > WWR:http://www.workingwithrails.com/person/9021-rick-denatale > LinkedIn:http://www.linkedin.com/in/rickdenatale > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users > > -- > You received this message because you are subscribed to the Google Groups "rspec" group. > To post to this group, send email to rspec at googlegroups.com. > To unsubscribe from this group, send email to rspec+unsubscribe at googlegroups.com. > For more options, visit this group athttp://groups.google.com/group/rspec?hl=en.