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.
76e01275c026bca5c46b2cb5e1ce174a?d=identicon&s=25 Mano ah (manoah)
on 2008-11-12 10: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
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2008-11-12 14:46
(Received via mailing list)
On Wed, Nov 12, 2008 at 3:18 AM, Mano ah <lists@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 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.
76e01275c026bca5c46b2cb5e1ce174a?d=identicon&s=25 Mano ah (manoah)
on 2008-11-13 12:18
Actually i need to test sending a barcode image and also test a value
returned by it.
Fbd9cb107fe7c941333d6a3488691989?d=identicon&s=25 Ramon Tayag (ramontayag)
on 2008-11-13 12: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 Tayag
76e01275c026bca5c46b2cb5e1ce174a?d=identicon&s=25 Mano ah (manoah)
on 2008-11-13 12:37
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.
Fbd9cb107fe7c941333d6a3488691989?d=identicon&s=25 Ramon Tayag (ramontayag)
on 2008-11-13 12: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 Tayag
76e01275c026bca5c46b2cb5e1ce174a?d=identicon&s=25 Mano ah (manoah)
on 2008-11-13 13:48
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 Tayag


Can I know what the create action handle.

I am getting undefined method 'create' error
Fbd9cb107fe7c941333d6a3488691989?d=identicon&s=25 Ramon Tayag (ramontayag)
on 2008-11-13 16: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 Tayag
76e01275c026bca5c46b2cb5e1ce174a?d=identicon&s=25 Mano ah (manoah)
on 2008-11-14 12: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
49de4cd2f26705785cbef2b15a9df7aa?d=identicon&s=25 Nick Hoffman (nickh)
on 2008-11-14 17: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
76e01275c026bca5c46b2cb5e1ce174a?d=identicon&s=25 Mano ah (manoah)
on 2008-11-18 07:53
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


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
76e01275c026bca5c46b2cb5e1ce174a?d=identicon&s=25 Mano ah (manoah)
on 2008-11-18 07: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
49de4cd2f26705785cbef2b15a9df7aa?d=identicon&s=25 Nick Hoffman (nickh)
on 2008-11-20 19: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 Chelimsky, Dan North, and other
experts have written on their blogs/sites.

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