I find myself doing this kind of thing a lot in Controller Specs: @vacancy = mock_model(Vacancy) @vacancy.stub!(:reference) @vacancy.stub!(:title) @vacancy.stub!(:created_at) @vacancy.stub!(:updated_at) @vacancy.stub!(:body) @vacancy.stub!(:contract) @vacancy.stub!(:location) @vacancy.stub!(:salary) @vacancy.stub!(:benefits) @vacancy.stub!(:start_date) @vacancy.stub!(:duration) I have to stub each of these methods so that the form renders correctly (as I am using integrate_views). This just feels *way* too much code and effort to do this. I have a spec for one controller (which just does plain old CRUD) that is over 300 LOC -- this seems a bit nuts to me when the controller is only 50 LOC. Does anybody have any suggestions on how to avoid this or perhaps I am doing something wrong!? I make use of a Factory pattern to create instances for my model unit tests -- could I use a similar pattern for a ''mock factory''? Is anybody doing anything similar? ~ Mark -- Posted via http://www.ruby-forum.com/.
On May 26, 2008, at 2:52 PM, Mark Dodwell wrote:> I find myself doing this kind of thing a lot in Controller Specs: > > @vacancy = mock_model(Vacancy) > @vacancy.stub!(:reference) > @vacancy.stub!(:title) > @vacancy.stub!(:created_at) > @vacancy.stub!(:updated_at) > @vacancy.stub!(:body) > @vacancy.stub!(:contract) > @vacancy.stub!(:location) > @vacancy.stub!(:salary) > @vacancy.stub!(:benefits) > @vacancy.stub!(:start_date) > @vacancy.stub!(:duration) > > I have to stub each of these methods so that the form renders > correctly > (as I am using integrate_views). This just feels *way* too much code > and > effort to do this. I have a spec for one controller (which just does > plain old CRUD) that is over 300 LOC -- this seems a bit nuts to me > when > the controller is only 50 LOC. > > Does anybody have any suggestions on how to avoid this or perhaps I am > doing something wrong!?Use stub_model instead (if you''re using source from github - if not the 1.4 release is coming soon).> > > I make use of a Factory pattern to create instances for my model unit > tests -- could I use a similar pattern for a ''mock factory''? Is > anybody > doing anything similar? > > ~ Mark > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users
On May 26, 2008, at 3:52 PM, Mark Dodwell wrote:> I find myself doing this kind of thing a lot in Controller Specs: > > @vacancy = mock_model(Vacancy) > @vacancy.stub!(:reference) > @vacancy.stub!(:title) > @vacancy.stub!(:created_at) > @vacancy.stub!(:updated_at) > @vacancy.stub!(:body) > @vacancy.stub!(:contract) > @vacancy.stub!(:location) > @vacancy.stub!(:salary) > @vacancy.stub!(:benefits) > @vacancy.stub!(:start_date) > @vacancy.stub!(:duration) > > I have to stub each of these methods so that the form renders > correctly > (as I am using integrate_views). This just feels *way* too much code > and > effort to do this. I have a spec for one controller (which just does > plain old CRUD) that is over 300 LOC -- this seems a bit nuts to me > when > the controller is only 50 LOC. > > Does anybody have any suggestions on how to avoid this or perhaps I am > doing something wrong!?I would highly suggest using :null_object => true in view specs (or controller specs with integrate_views): mock_model(MyARModel, :null_object => true, :to_s => "foobar") stubbing :to_s is also important in view specs. If you don''t stub to_s, to_s will be called on the mock object, and you''ll get invalid XHTML (see this ticket: http://rspec.lighthouseapp.com/projects/5645/tickets/230-mock-null_object-true-doesn-t-play-nice-with-html) Scott
On 26-mei-2008, at 21:52, Mark Dodwell wrote:> I find myself doing this kind of thing a lot in Controller Specs: > > @vacancy = mock_model(Vacancy) > @vacancy.stub!(:reference) > @vacancy.stub!(:title) > @vacancy.stub!(:created_at) > @vacancy.stub!(:updated_at) > @vacancy.stub!(:body) > @vacancy.stub!(:contract) > @vacancy.stub!(:location) > @vacancy.stub!(:salary) > @vacancy.stub!(:benefits) > @vacancy.stub!(:start_date) > @vacancy.stub!(:duration) > > I have to stub each of these methods so that the form renders > correctly > (as I am using integrate_views). This just feels *way* too much > code and > effort to do this. I have a spec for one controller (which just does > plain old CRUD) that is over 300 LOC -- this seems a bit nuts to me > when > the controller is only 50 LOC.Or, consider moving code into the model, and test it there. See http://weblog.jamisbuck.org/2006/10/18/skinny-controller-fat-model gr, bartz
Many thanks for your replies. David Chelimsky wrote:> Use stub_model instead (if you''re using source from github - if not > the 1.4 release is coming soon).Thanks - that helps a great deal. Scott Taylor wrote:> I would highly suggest using :null_object => true in view specs (or > controller specs with integrate_views):Thanks, that''s interesting, I didn''t know about that option... Are there any advantages of using this approach over #stub_model? Bart Zonneveld (bartz) wrote:> Or, consider moving code into the model, and test it there. See > http://weblog.jamisbuck.org/2006/10/18/skinny-controller-fat-modelI agree, but in this case the controller really only does CRUD, and it couldn''t really be any skinnier or it might die! Also, I don''t think that it is the main source of the problem here, rather the fact that I am using integrate_views which means I have to have a reasonably full mock of any models wherever I have a form for that model. ~ Mark -- Posted via http://www.ruby-forum.com/.
Also, any opinions on using a Factory pattern for instantiating mocks of models? I like the idea of maintaining mocks in one place as I frequently use them across several specs for models that have associations. Do you think it would create dependency issues between the specs using that Factory? ~ Mark -- Posted via http://www.ruby-forum.com/.
On 27.5.2008, at 10.52, Mark Dodwell wrote:> Also, any opinions on using a Factory pattern for instantiating > mocks of > models? I like the idea of maintaining mocks in one place as I > frequently use them across several specs for models that have > associations. Do you think it would create dependency issues between > the > specs using that Factory?I have built my own "factory" by creating methods such as mock_user in the spec helper. That method will then fill in some reasonable values and stub all the needed methods. Granted, the need for that is less now with stub_model, but sometimes you also want to stub some associations. That has certainly reduced the amount of repetition in my specs. //jarkko> > > ~ Mark > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users-- Jarkko Laine http://jlaine.net http://dotherightthing.com http://www.railsecommerce.com http://odesign.fi -------------- next part -------------- A non-text attachment was scrubbed... Name: smime.p7s Type: application/pkcs7-signature Size: 2417 bytes Desc: not available URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20080527/3ef9aaee/attachment.bin>
All the mentioned solutions are great, but for me they implied a bit too much hassle to set up and also, given a larger number of specs, a bit slow. This discussion actually motivated me to publish my own stubbing solution that I''ve recently implemented on GitHub. (This actually is a kind of a factory plus a dsl to define your stubs). http://github.com/svenfuchs/stubby/tree/master Like said in the readme, the code is pretty new and bugs/problems might well be present. Please let me know if you find any. On 27.05.2008, at 11:37, Jarkko Laine wrote:> > On 27.5.2008, at 10.52, Mark Dodwell wrote: > >> Also, any opinions on using a Factory pattern for instantiating >> mocks of >> models? I like the idea of maintaining mocks in one place as I >> frequently use them across several specs for models that have >> associations. Do you think it would create dependency issues >> between the >> specs using that Factory? > > I have built my own "factory" by creating methods such as mock_user > in the spec helper. That method will then fill in some reasonable > values and stub all the needed methods. Granted, the need for that > is less now with stub_model, but sometimes you also want to stub > some associations. That has certainly reduced the amount of > repetition in my specs. > > //jarkko > >> >> >> ~ Mark >> -- >> Posted via http://www.ruby-forum.com/. >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users > > -- > Jarkko Laine > http://jlaine.net > http://dotherightthing.com > http://www.railsecommerce.com > http://odesign.fi > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users-- sven fuchs svenfuchs at artweb-design.de artweb design http://www.artweb-design.de gr?nberger 65 + 49 (0) 30 - 47 98 69 96 (phone) d-10245 berlin + 49 (0) 171 - 35 20 38 4 (mobile)