Testing an action which requires picture


#1

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


#2

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/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.


#3

Actually i need to test sending a barcode image and also test a value
returned by it.


#4

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.


#5

You can put that barcode in the spec/fixtures directory. However,
you’ll need to manually place it there.

Ramon T.


#6

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.


#7

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


#8

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.


#9

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

#10

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


#11

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


#12

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


#13

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