[rspec-users] lots of nil problems!

Patrick J. Collins patrick at collinatorstudios.com
Fri Mar 19 17:35:54 EDT 2010


Hi,

I have this in my controller action:

from_address = Setting.find_by_name("shipping_from_address").address

and my spec fails:

NoMethodError in 'Admin::ShippingLabelsController should render a pdf file'
undefined method `address' for nil:NilClass

yet in the console I can do:

>>  Setting.find_by_name("shipping_from_address").address
=> #<Address id: 1970, label: .... etc>

and I get the address-- so I don't understand why it's failing in the spec.. ?

...

Also, if I want to test that I am getting a pdf back, how is the best way to do that?

I was doing:

controller:

   respond_to do |format|
     format.pdf do 
       render :template => 'admin/contacts/shipping_label'
    end

spec:

  def do_get
    get :create, :format => :pdf
  end

  it "should render a pdf file" do
    do_get
    response.should render_template("admin/contacts/shipping_label")
  end

-- but that gives me:
'Admin::ShippingLabelsController should render a pdf file' FAILED
expected "admin/contacts/shipping_label", got nil
/Users/patrick/coding/rails/matthew/spec/controllers/admin/shipping_labels_controller_spec.rb:11:

thanks...

Patrick J. Collins
http://collinatorstudios.com




More information about the rspec-users mailing list