Forum: RSpec testing an action which requires picture

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Mano a. (Guest)
on 2008-11-12 11:18
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
David C. (Guest)
on 2008-11-12 15:46
(Received via mailing list)
On Wed, Nov 12, 2008 at 3:18 AM, Mano ah <removed_email_address@domain.invalid> 
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 correct

I'm pretty sure you're looking for this:

http://api.rubyonrails.com/classes/ActionControlle...

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.
Mano a. (Guest)
on 2008-11-13 13:18
Actually i need to test sending a barcode image and also test a value
returned by it.
Ramon T. (Guest)
on 2008-11-13 13:26
(Received via mailing list)
You can put that barcode in the spec/fixtures directory. However,
you'll need to manually place it there.

Ramon T.
Mano a. (Guest)
on 2008-11-13 13:37
Ramon T. wrote:
> You can put that barcode in the spec/fixtures directory. However,
> you'll need to manually place it there.
>
> Ramon T.

ok. Also please can i know how to test sending an image using rspec.
Ramon T. (Guest)
on 2008-11-13 13:55
(Received via mailing list)
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 T.
Mano a. (Guest)
on 2008-11-13 14:48
Ramon T. 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 T.


Can I know what the create action handle.

I am getting undefined method 'create' error
Ramon T. (Guest)
on 2008-11-13 17:26
(Received via mailing list)
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 T.
Mano a. (Guest)
on 2008-11-14 13:29
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
Nick H. (Guest)
on 2008-11-14 18:58
(Received via mailing list)
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
Mano a. (Guest)
on 2008-11-18 08:53
Nick H. 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


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 blog image value
      end

    end


What is the rspec code to test the return value and make it pass
Mano a. (Guest)
on 2008-11-18 08:55

 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
Nick H. (Guest)
on 2008-11-20 20:09
(Received via mailing list)
On 2008-11-18, at 01:53, Mano ah wrote:
>>
>>
>> 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.

>      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

The 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 C., Dan N., and other
experts have written on their blogs/sites.

Cheers,
Nick
This topic is locked and can not be replied to.