hello spec''ers, i''m in the hunt for guides on testing controllers with rspec, would you guys recommend any? thanks Oliver -- Oliver Azevedo Barnes oli.azevedo.barnes at gmail.com +55 11 9768 0193 http://www.linkedin.com/in/oliverbarnes http://workingwithrails.com/person/4704-oliver-barnes
On Mon, Mar 10, 2008 at 2:01 PM, Oliver Barnes <oli.azevedo.barnes at gmail.com> wrote:> hello spec''ers, > > i''m in the hunt for guides on testing controllers with rspec, would > you guys recommend any?Oldie but goodie (I hope) - http://evang.eli.st/blog/2007/9/15/easy-controller-tests-and-expressing-intent-through-expectations Pat
thanks pat, it''s still a good primer ;) I''m getting a better handle at it, but I''m still confused as to how to test assigment of associated objects. for instance, how do I test this (using attachment_fu)? @work.image = @image 2008/3/11, Pat Maddox <pergesu at gmail.com>:> On Mon, Mar 10, 2008 at 2:01 PM, Oliver Barnes > <oli.azevedo.barnes at gmail.com> wrote: > > hello spec''ers, > > > > i''m in the hunt for guides on testing controllers with rspec, would > > you guys recommend any? > > > Oldie but goodie (I hope) - > http://evang.eli.st/blog/2007/9/15/easy-controller-tests-and-expressing-intent-through-expectations > > Pat > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >-- Oliver Azevedo Barnes oli.azevedo.barnes at gmail.com +55 11 9768 0193 http://www.linkedin.com/in/oliverbarnes http://workingwithrails.com/person/4704-oliver-barnes
On Thu, Mar 13, 2008 at 3:54 PM, Oliver Barnes <oli.azevedo.barnes at gmail.com> wrote:> thanks pat, it''s still a good primer ;) > > I''m getting a better handle at it, but I''m still confused as to how to > test assigment of associated objects. for instance, how do I test this > (using attachment_fu)? > > @work.image = @imageYou could either use an interaction-based or state-based test. it "should assign the work image" do @mock_work.should_receive(:image=).with(@mock_image) end it "should assign the work image" do Work.find(3).image.should_not be_nil end The basic idea is that you''re specifying that it receives a method call that you know works, or you can verify some state that should be true once the action has taken place. hth Pat
I see. I had gotten to trying the first way you suggested (haven''t tried the second yet): it "should assign an image to the work" do @work.should_receive(:image=).with(@image) end but I got the following error: should assign an image to the work Mock ''Work_1002'' expected :image= with (#<Image:0x19e8094 @name="Image_1001">) once, but received it 0 times though in the actual controller @work does receive it: def create @work = @category.works.build(params[:work]) @image = Image.new(params[:image]) @work.image = @image respond_to do |format| if @image.save and @work.save flash[:notice] = ''Work was successfully created.'' format.html { redirect_to admin_category_work_path(@category, at work) } else format.html { render :action => "new" } end end end I''m sure I''m missing something here... 2008/3/13, Pat Maddox <pergesu at gmail.com>:> On Thu, Mar 13, 2008 at 3:54 PM, Oliver Barnes > > <oli.azevedo.barnes at gmail.com> wrote: > > > thanks pat, it''s still a good primer ;) > > > > I''m getting a better handle at it, but I''m still confused as to how to > > test assigment of associated objects. for instance, how do I test this > > (using attachment_fu)? > > > > @work.image = @image > > > You could either use an interaction-based or state-based test. > > it "should assign the work image" do > @mock_work.should_receive(:image=).with(@mock_image) > end > > it "should assign the work image" do > Work.find(3).image.should_not be_nil > end > > The basic idea is that you''re specifying that it receives a method > call that you know works, or you can verify some state that should be > true once the action has taken place. > > hth > > > Pat > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >-- Oliver Azevedo Barnes oli.azevedo.barnes at gmail.com +55 11 9768 0193 http://www.linkedin.com/in/oliverbarnes http://workingwithrails.com/person/4704-oliver-barnes
Are u stubbing Image.new and returning @image?? otherwise I think the error is accurately complaining that it did not receive :image= with the object that you have said. Cheers Shane On 14/03/2008, at 12:55 PM, Oliver Barnes wrote:> I see. I had gotten to trying the first way you suggested (haven''t > tried the second yet): > > it "should assign an image to the work" do > @work.should_receive(:image=).with(@image) > end > > but I got the following error: > > should assign an image to the work > Mock ''Work_1002'' expected :image= with (#<Image:0x19e8094 > @name="Image_1001">) once, but received it 0 times > > though in the actual controller @work does receive it: > > def create > @work = @category.works.build(params[:work]) > @image = Image.new(params[:image]) > @work.image = @image > respond_to do |format| > if @image.save and @work.save > flash[:notice] = ''Work was successfully created.'' > format.html { redirect_to > admin_category_work_path(@category, at work) } > else > format.html { render :action => "new" } > end > end > end > > I''m sure I''m missing something here... > > > 2008/3/13, Pat Maddox <pergesu at gmail.com>: >> On Thu, Mar 13, 2008 at 3:54 PM, Oliver Barnes >> >> <oli.azevedo.barnes at gmail.com> wrote: >> >>> thanks pat, it''s still a good primer ;) >>> >>> I''m getting a better handle at it, but I''m still confused as to >>> how to >>> test assigment of associated objects. for instance, how do I test >>> this >>> (using attachment_fu)? >>> >>> @work.image = @image >> >> >> You could either use an interaction-based or state-based test. >> >> it "should assign the work image" do >> @mock_work.should_receive(:image=).with(@mock_image) >> end >> >> it "should assign the work image" do >> Work.find(3).image.should_not be_nil >> end >> >> The basic idea is that you''re specifying that it receives a method >> call that you know works, or you can verify some state that should be >> true once the action has taken place. >> >> hth >> >> >> Pat >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users >> > > > -- > Oliver Azevedo Barnes > oli.azevedo.barnes at gmail.com > +55 11 9768 0193 > http://www.linkedin.com/in/oliverbarnes > http://workingwithrails.com/person/4704-oliver-barnes > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-usersShane Mingins ELC Technologies (TM) 1921 State Street Santa Barbara, CA 93101 Phone: +64 4 568 6684 Mobile: +64 21 435 586 Email: smingins at elctech.com AIM: ShaneMingins Skype: shane.mingins (866) 863-7365 Tel - Santa Barbara Office (866) 893-1902 Fax - Santa Barbara Office +44 020 7504 1346 Tel - London Office +44 020 7504 1347 Fax - London Office http://www.elctech.com -------------------------------------------------------------------- Privacy and Confidentiality Notice: The information contained in this electronic mail message is intended for the named recipient(s) only. It may contain privileged and confidential information. If you are not an intended recipient, you must not copy, forward, distribute or take any action in reliance on it. If you have received this electronic mail message in error, please notify the sender immediately.
And the other thing I am not seeing .... you are calling create somewhere in the spec? Always a good idea to post all the code :-) Cheers Shane On 14/03/2008, at 12:55 PM, Oliver Barnes wrote:> I see. I had gotten to trying the first way you suggested (haven''t > tried the second yet): > > it "should assign an image to the work" do > @work.should_receive(:image=).with(@image) > end > > but I got the following error: > > should assign an image to the work > Mock ''Work_1002'' expected :image= with (#<Image:0x19e8094 > @name="Image_1001">) once, but received it 0 times > > though in the actual controller @work does receive it: > > def create > @work = @category.works.build(params[:work]) > @image = Image.new(params[:image]) > @work.image = @image > respond_to do |format| > if @image.save and @work.save > flash[:notice] = ''Work was successfully created.'' > format.html { redirect_to > admin_category_work_path(@category, at work) } > else > format.html { render :action => "new" } > end > end > end > > I''m sure I''m missing something here... > > > 2008/3/13, Pat Maddox <pergesu at gmail.com>: >> On Thu, Mar 13, 2008 at 3:54 PM, Oliver Barnes >> >> <oli.azevedo.barnes at gmail.com> wrote: >> >>> thanks pat, it''s still a good primer ;) >>> >>> I''m getting a better handle at it, but I''m still confused as to >>> how to >>> test assigment of associated objects. for instance, how do I test >>> this >>> (using attachment_fu)? >>> >>> @work.image = @image >> >> >> You could either use an interaction-based or state-based test. >> >> it "should assign the work image" do >> @mock_work.should_receive(:image=).with(@mock_image) >> end >> >> it "should assign the work image" do >> Work.find(3).image.should_not be_nil >> end >> >> The basic idea is that you''re specifying that it receives a method >> call that you know works, or you can verify some state that should be >> true once the action has taken place. >> >> hth >> >> >> Pat >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users >> > > > -- > Oliver Azevedo Barnes > oli.azevedo.barnes at gmail.com > +55 11 9768 0193 > http://www.linkedin.com/in/oliverbarnes > http://workingwithrails.com/person/4704-oliver-barnes > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-usersShane Mingins ELC Technologies (TM) 1921 State Street Santa Barbara, CA 93101 Phone: +64 4 568 6684 Mobile: +64 21 435 586 Email: smingins at elctech.com AIM: ShaneMingins Skype: shane.mingins (866) 863-7365 Tel - Santa Barbara Office (866) 893-1902 Fax - Santa Barbara Office +44 020 7504 1346 Tel - London Office +44 020 7504 1347 Fax - London Office http://www.elctech.com -------------------------------------------------------------------- Privacy and Confidentiality Notice: The information contained in this electronic mail message is intended for the named recipient(s) only. It may contain privileged and confidential information. If you are not an intended recipient, you must not copy, forward, distribute or take any action in reliance on it. If you have received this electronic mail message in error, please notify the sender immediately.
On Thu, Mar 13, 2008 at 4:55 PM, Oliver Barnes <oli.azevedo.barnes at gmail.com> wrote:> I see. I had gotten to trying the first way you suggested (haven''t > tried the second yet): > > it "should assign an image to the work" do > @work.should_receive(:image=).with(@image) > end > > but I got the following error: > > should assign an image to the work > Mock ''Work_1002'' expected :image= with (#<Image:0x19e8094 > @name="Image_1001">) once, but received it 0 times > > though in the actual controller @work does receive it: > > def create > @work = @category.works.build(params[:work]) > @image = Image.new(params[:image]) > @work.image = @image > respond_to do |format| > if @image.save and @work.save > flash[:notice] = ''Work was successfully created.'' > format.html { redirect_to admin_category_work_path(@category, at work) } > else > format.html { render :action => "new" } > end > end > end > > I''m sure I''m missing something here...In all likelihood, you''re not stubbing the call to @category.works.build, and instead are just letting Rails do its thing. You need to take control of the objects if you want to make interaction verifications of them. Read through some of the documentation starting at http://rspec.info/documentation/mocks/ and see if you can grok it. Feel free to ask more questions if you run into trouble. Pat
should have posted the code, sorry about that :) http://pastie.caboo.se/165708 I was actually posting to :create, but hadn''t added do_post() to the example :P adding it solved this specific problem, thanks. hope the pastie helps another newb out 2008/3/13, Shane Mingins <smingins at elctech.com>:> And the other thing I am not seeing .... you are calling create > somewhere in the spec? > > Always a good idea to post all the code :-) > > > Cheers > Shane > > > On 14/03/2008, at 12:55 PM, Oliver Barnes wrote: > > > > I see. I had gotten to trying the first way you suggested (haven''t > > tried the second yet): > > > > it "should assign an image to the work" do > > @work.should_receive(:image=).with(@image) > > end > > > > but I got the following error: > > > > should assign an image to the work > > Mock ''Work_1002'' expected :image= with (#<Image:0x19e8094 > > @name="Image_1001">) once, but received it 0 times > > > > though in the actual controller @work does receive it: > > > > def create > > @work = @category.works.build(params[:work]) > > @image = Image.new(params[:image]) > > @work.image = @image > > respond_to do |format| > > if @image.save and @work.save > > flash[:notice] = ''Work was successfully created.'' > > format.html { redirect_to > > admin_category_work_path(@category, at work) } > > else > > format.html { render :action => "new" } > > end > > end > > end > > > > I''m sure I''m missing something here... > > > > > > 2008/3/13, Pat Maddox <pergesu at gmail.com>: > >> On Thu, Mar 13, 2008 at 3:54 PM, Oliver Barnes > >> > >> <oli.azevedo.barnes at gmail.com> wrote: > >> > >>> thanks pat, it''s still a good primer ;) > >>> > >>> I''m getting a better handle at it, but I''m still confused as to > >>> how to > >>> test assigment of associated objects. for instance, how do I test > >>> this > >>> (using attachment_fu)? > >>> > >>> @work.image = @image > >> > >> > >> You could either use an interaction-based or state-based test. > >> > >> it "should assign the work image" do > >> @mock_work.should_receive(:image=).with(@mock_image) > >> end > >> > >> it "should assign the work image" do > >> Work.find(3).image.should_not be_nil > >> end > >> > >> The basic idea is that you''re specifying that it receives a method > >> call that you know works, or you can verify some state that should be > >> true once the action has taken place. > >> > >> hth > >> > >> > >> Pat > >> _______________________________________________ > >> rspec-users mailing list > >> rspec-users at rubyforge.org > >> http://rubyforge.org/mailman/listinfo/rspec-users > >> > > > > > > -- > > Oliver Azevedo Barnes > > oli.azevedo.barnes at gmail.com > > +55 11 9768 0193 > > http://www.linkedin.com/in/oliverbarnes > > http://workingwithrails.com/person/4704-oliver-barnes > > _______________________________________________ > > rspec-users mailing list > > rspec-users at rubyforge.org > > http://rubyforge.org/mailman/listinfo/rspec-users > > > Shane Mingins > ELC Technologies (TM) > 1921 State Street > Santa Barbara, CA 93101 > > > Phone: +64 4 568 6684 > Mobile: +64 21 435 586 > Email: smingins at elctech.com > AIM: ShaneMingins > Skype: shane.mingins > > (866) 863-7365 Tel - Santa Barbara Office > (866) 893-1902 Fax - Santa Barbara Office > > +44 020 7504 1346 Tel - London Office > +44 020 7504 1347 Fax - London Office > > http://www.elctech.com > > -------------------------------------------------------------------- > Privacy and Confidentiality Notice: > The information contained in this electronic mail message is intended > for the named recipient(s) only. It may contain privileged and > confidential information. If you are not an intended recipient, you > must not copy, forward, distribute or take any action in reliance on > it. If you have received this electronic mail message in error, please > notify the sender immediately. > > _______________________________________________ > > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >-- Oliver Azevedo Barnes oli.azevedo.barnes at gmail.com +55 11 9768 0193 http://www.linkedin.com/in/oliverbarnes http://workingwithrails.com/person/4704-oliver-barnes
thanks, will do - I am still wrapping my mind around mocks and stubs indeed :) 2008/3/13, Pat Maddox <pergesu at gmail.com>:> On Thu, Mar 13, 2008 at 4:55 PM, Oliver Barnes > > <oli.azevedo.barnes at gmail.com> wrote: > > > I see. I had gotten to trying the first way you suggested (haven''t > > tried the second yet): > > > > it "should assign an image to the work" do > > @work.should_receive(:image=).with(@image) > > end > > > > but I got the following error: > > > > should assign an image to the work > > Mock ''Work_1002'' expected :image= with (#<Image:0x19e8094 > > @name="Image_1001">) once, but received it 0 times > > > > though in the actual controller @work does receive it: > > > > def create > > @work = @category.works.build(params[:work]) > > @image = Image.new(params[:image]) > > @work.image = @image > > respond_to do |format| > > if @image.save and @work.save > > flash[:notice] = ''Work was successfully created.'' > > format.html { redirect_to admin_category_work_path(@category, at work) } > > else > > format.html { render :action => "new" } > > end > > end > > end > > > > I''m sure I''m missing something here... > > > In all likelihood, you''re not stubbing the call to > @category.works.build, and instead are just letting Rails do its > thing. You need to take control of the objects if you want to make > interaction verifications of them. > > Read through some of the documentation starting at > http://rspec.info/documentation/mocks/ and see if you can grok it. > Feel free to ask more questions if you run into trouble. > > > Pat > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >-- Oliver Azevedo Barnes oli.azevedo.barnes at gmail.com +55 11 9768 0193 http://www.linkedin.com/in/oliverbarnes http://workingwithrails.com/person/4704-oliver-barnes
I got the spec and controller minimally working now, here''s the code paste for both in full. http://pastie.caboo.se/165743 still working on it, any suggestions on possible improvements would be greatly appreciated. 2008/3/14, Oliver Barnes <oli.azevedo.barnes at gmail.com>:> thanks, will do - I am still wrapping my mind around mocks and stubs indeed :) > > > 2008/3/13, Pat Maddox <pergesu at gmail.com>: > > > On Thu, Mar 13, 2008 at 4:55 PM, Oliver Barnes > > > > <oli.azevedo.barnes at gmail.com> wrote: > > > > > I see. I had gotten to trying the first way you suggested (haven''t > > > tried the second yet): > > > > > > it "should assign an image to the work" do > > > @work.should_receive(:image=).with(@image) > > > end > > > > > > but I got the following error: > > > > > > should assign an image to the work > > > Mock ''Work_1002'' expected :image= with (#<Image:0x19e8094 > > > @name="Image_1001">) once, but received it 0 times > > > > > > though in the actual controller @work does receive it: > > > > > > def create > > > @work = @category.works.build(params[:work]) > > > @image = Image.new(params[:image]) > > > @work.image = @image > > > respond_to do |format| > > > if @image.save and @work.save > > > flash[:notice] = ''Work was successfully created.'' > > > format.html { redirect_to admin_category_work_path(@category, at work) } > > > else > > > format.html { render :action => "new" } > > > end > > > end > > > end > > > > > > I''m sure I''m missing something here... > > > > > > In all likelihood, you''re not stubbing the call to > > @category.works.build, and instead are just letting Rails do its > > thing. You need to take control of the objects if you want to make > > interaction verifications of them. > > > > Read through some of the documentation starting at > > http://rspec.info/documentation/mocks/ and see if you can grok it. > > Feel free to ask more questions if you run into trouble. > > > > > > Pat > > _______________________________________________ > > rspec-users mailing list > > rspec-users at rubyforge.org > > http://rubyforge.org/mailman/listinfo/rspec-users > > > > > > -- > Oliver Azevedo Barnes > > oli.azevedo.barnes at gmail.com > > +55 11 9768 0193 > http://www.linkedin.com/in/oliverbarnes > http://workingwithrails.com/person/4704-oliver-barnes >-- Oliver Azevedo Barnes oli.azevedo.barnes at gmail.com +55 11 9768 0193 http://www.linkedin.com/in/oliverbarnes http://workingwithrails.com/person/4704-oliver-barnes
Seemingly Similar Threads
- Question about a AA result and its use in Dependence Analysis
- Question about a AA result and its use in Dependence Analysis
- Question about a AA result and its use in Dependence Analysis
- Dependence Analysis bug or undefined behavior?
- Question about a AA result and its use in Dependence Analysis