How can i test the action of a controller which requires picture? I mean, as part of the test I want to pass a picture (somekind of fixture) and test to see if the result is correct -- Posted via http://www.ruby-forum.com/.
David Chelimsky
2008-Nov-12 13:40 UTC
[rspec-users] testing an action which requires picture
On Wed, Nov 12, 2008 at 3:18 AM, Mano ah <lists at ruby-forum.com> wrote:> How can i test the action of a controller which requires picture? > > I mean, as part of the test I want to pass a picture (somekind of > fixture) and test to see if the result is correctI''m pretty sure you''re looking for this: http://api.rubyonrails.com/classes/ActionController/TestProcess.html#M000229 You can use fixture_file_upload with rspec-rails. In fact, you can use any of rails'' test/unit helpers and extensions when working with rspec-rails.
Actually i need to test sending a barcode image and also test a value returned by it. -- Posted via http://www.ruby-forum.com/.
You can put that barcode in the spec/fixtures directory. However, you''ll need to manually place it there. Ramon Tayag On Thu, Nov 13, 2008 at 7:18 PM, Mano ah <lists at ruby-forum.com> wrote:> Actually i need to test sending a barcode image and also test a value > returned by it. > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Ramon Tayag wrote:> You can put that barcode in the spec/fixtures directory. However, > you''ll need to manually place it there. > > Ramon Tayagok. Also please can i know how to test sending an image using rspec. -- Posted via http://www.ruby-forum.com/.
I suggest doing those tests in the model, not in the controller. Just do that fixture thing for the controller spec to pass. But pass the same thing in the model. Here''s a sample of how I do it require File.expand_path(File.dirname(__FILE__) + ''/../spec_helper'') describe ProductImage do before(:all) do @valid_attributes = { :uploaded_data => fixture_file_upload("100x100.jpg",''image/jpg''), :product_id => 1 } @product_image = ProductImage.create(@valid_attributes) end it "should create a new instance given valid attributes" do ProductImage.create!(@valid_attributes) end end Ramon Tayag On Thu, Nov 13, 2008 at 7:37 PM, Mano ah <lists at ruby-forum.com> wrote:> Ramon Tayag wrote: >> You can put that barcode in the spec/fixtures directory. However, >> you''ll need to manually place it there. >> >> Ramon Tayag > > ok. Also please can i know how to test sending an image using rspec. > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Ramon Tayag wrote:> I suggest doing those tests in the model, not in the controller. Just > do that fixture thing for the controller spec to pass. But pass the > same thing in the model. Here''s a sample of how I do it > > require File.expand_path(File.dirname(__FILE__) + ''/../spec_helper'') > > describe ProductImage do > before(:all) do > @valid_attributes = { > :uploaded_data => fixture_file_upload("100x100.jpg",''image/jpg''), > :product_id => 1 > } > @product_image = ProductImage.create(@valid_attributes) > end > > it "should create a new instance given valid attributes" do > ProductImage.create!(@valid_attributes) > end > end > > Ramon TayagCan I know what the create action handle. I am getting undefined method ''create'' error -- Posted via http://www.ruby-forum.com/.
Hmm.. not sure what that error is. The veterans in here might be able to help you. The create action should work... btw, this is a model spec, not a controller spec, just in case you''re a newbie. Ramon Tayag On Thu, Nov 13, 2008 at 8:48 PM, Mano ah <lists at ruby-forum.com> wrote:> Can I know what the create action handle. > > I am getting undefined method ''create'' error
can i know how to test a picture upload which dosent interact with db my code is def scan #------- if request.post? image = Image.new image.blob= params[:image][:blob] image.save_picture end -- Posted via http://www.ruby-forum.com/.
On 2008-11-14, at 06:29, Mano ah wrote:> can i know how to test a picture upload which dosent interact with db > > my code is > > > def scan > #------- > > if request.post? > > image = Image.new > > image.blob= params[:image][:blob] > > image.save_picture > > end > --Hi Mano. It doesn''t really matter whether or not your "picture" model interacts with a database. Simply use mocks and stubs when speccing #scan , and you''ll be good. For example: describe ''#scan'' do describe ''receives a POST request'' do before :each do # mock an Image instance # stub Image#new end it ''should create a new Image'' it "should set the new image''s ''blob'' attribute" it ''should save the new image'' end describe ''receives a non-POST request'' do # stuff here end end Obviously you have to fill in the contents of those #it blocks, but that''s really all you need. -Nick
Nick Hoffman wrote:> On 2008-11-14, at 06:29, Mano ah wrote: >> image = Image.new >> >> image.blob= params[:image][:blob] >> >> image.save_picture >> >> end >> -- > > Hi Mano. It doesn''t really matter whether or not your "picture" model > interacts with a database. Simply use mocks and stubs when speccing > #scan , and you''ll be good. For example: > > describe ''#scan'' do > describe ''receives a POST request'' do > before :each do > # mock an Image instance > # stub Image#new > end > > it ''should create a new Image'' > it "should set the new image''s ''blob'' attribute" > it ''should save the new image'' > end > > describe ''receives a non-POST request'' do > # stuff here > end > end > > Obviously you have to fill in the contents of those #it blocks, but > that''s really all you need. > -NickThank you All the above specification passed. Now I want to test the return value. I mean def scan #------- if request.post? image = Image.new image.blob= params[:image][:blob] if image.save_picture @code = returns a blog image value end end What is the rspec code to test the return value and make it pass -- Posted via http://www.ruby-forum.com/.
Thank you All the above specification passed. Now I want to test the return value. I mean def scan #------- if request.post? image = Image.new image.blob= params[:image][:blob] if image.save_picture @code = returns a barcode image value end end What is the rspec code to test the return value and make it pass -- Posted via http://www.ruby-forum.com/.
On 2008-11-18, at 01:53, Mano ah wrote:> Nick Hoffman wrote: >> On 2008-11-14, at 06:29, Mano ah wrote: >>> image = Image.new >>> >>> image.blob= params[:image][:blob] >>> >>> image.save_picture >>> >>> end >>> -- >> >> Hi Mano. It doesn''t really matter whether or not your "picture" model >> interacts with a database. Simply use mocks and stubs when speccing >> #scan , and you''ll be good. For example: >> >> describe ''#scan'' do >> describe ''receives a POST request'' do >> before :each do >> # mock an Image instance >> # stub Image#new >> end >> >> it ''should create a new Image'' >> it "should set the new image''s ''blob'' attribute" >> it ''should save the new image'' >> end >> >> describe ''receives a non-POST request'' do >> # stuff here >> end >> end >> >> Obviously you have to fill in the contents of those #it blocks, but >> that''s really all you need. >> -Nick >Hi Mano.> All the above specification passed.Um, that''s because those calls to #it were meant as a guide for you, and don''t actually contain any real tests. You need to write those yourself.> Now I want to test the return value. I mean > > > def scan > #------- > > if request.post? > > image = Image.new > > image.blob= params[:image][:blob] > > if image.save_picture > > @code = returns a blog image value > end > > end > > > What is the rspec code to test the return value and make it passThe code above is trivial to spec if you understand RSpec. It sounds like you need to learn more about RSpec and behaviour-driven development. I recommend searching Google for a couple of articles/ guides, as well as reading what David Chelimsky, Dan North, and other experts have written on their blogs/sites. Cheers, Nick