> Anyway, I think all you need to do is provoke this behaviour in your > request (i.e. convince datamapper's Resource#destroy to return > fals/nil), and then test for response.status.should == 500
I did this using mocks. In my mock, destroy returns false. Otherwise, how to convince Resource#destroy to not return true? :( I really don't want to use mocks in my tests, but, this is only the place I don't know how to get rid of it. Maybe, in this case, mocking is the best solution? I'm not sure if this line of code *really* needs testing, what do you think? I'm testing this, bacause, I want to test if it raises an error on those cases. Here I'm testing for InternalServerError, but I could raise my own Exception, like: ReferentialIntegrityError. Arthur Zapparoli http://merb-br.org http://arthurgeek.net On Sun, Dec 14, 2008 at 3:44 AM, Martin Gamsjaeger <[email protected]> wrote: > > Arthur, > > You're right, I only test for NotFound. That's mainly because I really > forgot about InternalServerError in the first place :) > > Looking at > > http://github.com/sam/dm-core/tree/master/lib/dm-core/resource.rb#L300 > http://github.com/sam/dm-core/tree/master/lib/dm-core/repository.rb#L79 > > it seems, that destroying a new_record? returns false, and any kind of > database error would also return false. I guess some kind of foreign > key error or something could be a reason? > > Anyway, I think all you need to do is provoke this behaviour in your > request (i.e. convince datamapper's Resource#destroy to return > fals/nil), and then test for response.status.should == 500 > > cheers > snusnu > > On Sun, Dec 14, 2008 at 06:16, Arthur Zapparoli <[email protected]> wrote: >> >> Martin, >> >> Correct me if I'm wrong, but, in your tests: you just test the DELETE >> if there's no record. Right? So, it raises NotFound and return a 404 >> status. >> >> http://github.com/snusnu/merb_resource_controller/tree/master/spec/mrc_test_app/spec/request/comments_spec.rb#L209 >> >> But, there's two kind of failures in this case: >> >> - A record exists >> - No record exists (or, no record could be found with this ID) >> >> You are testing the latter, and I want to test the former. >> >> Here's my controller (same code merb-gen gives me): >> >> def destroy(id) >> @foo = Foo.get(id) >> raise NotFound unless @foo >> if @foo.destroy >> redirect resource(:foos) >> else >> raise InternalServerError >> end >> end >> >> I wan't to test the raise InternalServerError case. >> I already have specs for the raise NotFound line. And without any mocks. :) >> >> Arthur Zapparoli >> http://merb-br.org >> http://arthurgeek.net >> >> >> >> On Sun, Dec 14, 2008 at 3:01 AM, Martin Gamsjaeger <[email protected]> >> wrote: >>> >>> Arthur, >>> >>> I forgot to mention that the default controllers that get generated by >>> merb-gen resource don't return NotAcceptable (406) on failing >>> requests. You will have to add that yourself to the actions in >>> question. Again, have a look at >>> http://github.com/snusnu/merb_resource_controller/tree/master/lib/merb_resource_controller/actions.rb >>> to see how I do that. Basically, it boils down to providing the >>> appropriate :status option to render/display >>> >>> cheers >>> snusnu >>> >>> On Sun, Dec 14, 2008 at 05:57, Martin Gamsjaeger <[email protected]> >>> wrote: >>>> Hey Arthur, >>>> >>>> Have a look at the specs inside >>>> http://github.com/snusnu/merb_resource_controller/tree/master to see >>>> how I do this. Not sure if that's the best, but it basically relies on >>>> returning appropriate HTTP status codes so it should be quite ok. >>>> >>>> cheers >>>> snusnu >>>> >>>> On Sun, Dec 14, 2008 at 04:27, Arthur Zapparoli <[email protected]> >>>> wrote: >>>>> >>>>> In my sucessfull DELETE, there's no mock. In fact, I don't use mocks >>>>> on all of my other tests. I know how this can make my tests brittle. >>>>> >>>>> But I couldn't figure how to test this: "if anything goes wrong with >>>>> the DELETE, raise InternalServerError" without mocking. >>>>> >>>>> And, merb-gen do not creates tests for failing. It only create tests >>>>> for successful delete. >>>>> >>>>> Arthur Zapparoli >>>>> http://merb-br.org >>>>> http://arthurgeek.net >>>>> >>>>> On Sun, Dec 14, 2008 at 12:52 AM, Tony Mann <[email protected]> wrote: >>>>>> You don't need to use mocks. To see how to do what you want, use merb-gen >>>>>> resource_controller to make controller, and then look at its specs. The >>>>>> specs use pure CRUD to create and delete resources, making it a more >>>>>> realistic test of the system. >>>>>> >>>>>> The merb community frowns on mocks, and once you ditch them, you will see >>>>>> why. Suddenly your tests fail when the actual system fails! >>>>>> >>>>>> ..tony.. >>>>>> >>>>>> On Sat, Dec 13, 2008 at 2:25 PM, Arthur Zapparoli <[email protected]> >>>>>> wrote: >>>>>>> >>>>>>> Hi everyone, >>>>>>> >>>>>>> How to test a failing request on my controllers? >>>>>>> >>>>>>> Eg: I want to test a sucessfull DELETE, and a failing DELETE too. >>>>>>> >>>>>>> I did this using mocks, but I wan't to know if there's any othr way to >>>>>>> test this. >>>>>>> >>>>>>> Here's my code: >>>>>>> >>>>>>> describe "a failing DELETE", :given => "a foo exists" do >>>>>>> >>>>>>> before(:each) do >>>>>>> Foo.should_receive(:get).and_return(@foo = Foo.first) >>>>>>> @foo.should_receive(:destroy).and_return(false) >>>>>>> end >>>>>>> >>>>>>> it "do not deletes the foo from the database" do >>>>>>> lambda { delete_neighborhood(Foo.first) }.should_not change { >>>>>>> Foo.count } >>>>>>> end >>>>>>> >>>>>>> it "raises a 500 InternalServerError" do >>>>>>> @response = delete_neighborhood(Foo.first) >>>>>>> @response.status.should == 500 >>>>>>> end >>>>>>> end >>>>>>> >>>>>>> And, controller: >>>>>>> >>>>>>> def destroy(id) >>>>>>> @foo = Foo.get(id) >>>>>>> raise NotFound unless @foo >>>>>>> if @foo.destroy >>>>>>> redirect resource(:foos), :message => {:notice => "Foo >>>>>>> sucessfully deleted"} >>>>>>> else >>>>>>> raise InternalServerError >>>>>>> end >>>>>>> end >>>>>>> >>>>>>> >>>>>>> Arthur Zapparoli >>>>>>> http://merb-br.org >>>>>>> http://arthurgeek.net >>>>>>> >>>>>>> >>>>>> >>>>>> >>>>>> > >>>>>> >>>>> >>>>> >> >>>>> >>>> >>> >>> > >>> >> >> > >> > > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "merb" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/merb?hl=en -~----------~----~----~----~------~----~------~--~---
