2

I'm trying to post to my controller in RSPEC, see anything wrong with this? It's failing w/o error:

it "should store create an IncomingMail record" do
  lambda {      
    post 'create', {
           "from" => 'XXX',
           "to" => 'XXX',
           "cc" => 'XXX', 
           "subject" => 'XXX', 
           "message_text" => 'XXX',
           "message_html" => 'XXX' }
  }.should change { IncomingMail.count }.by(1)
end

Updated:

it "should store create an IncomingMail record" do
  post :create,
       :from => 'xx',
       :to => 'xx',
       :cc => 'xx',
       :subject => 'xx',
       :message_text => 'xx',
       :message_html => 'xx'

  mail = IncomingMail.last(:order => 'created_at desc')
  mail.from.should == 'xx'
end

Controller

class IncomingMailsController < ApplicationController

  require 'iconv'

  #make sure that rails doesn't raise an exception because we have no way of knowing the token
  skip_before_filter :verify_authenticity_token

  def create

    begin
      @incoming_mail = IncomingMail.create(
                                            :from => params[:from],
                                            :to => params[:to],
                                            :cc => params[:cc], 
                                            :subject => params[:subject], 
                                            :message_text => message_text_utf8,
                                            :message_html => message_html_utf8
                                            )
.....
9
  • Added an updated approach. Still it is failing. In the test.log file I see the record being created, but then it shows: SQL (0.1ms) RELEASE SAVEPOINT active_record_1 SQL (0.3ms) SELECT COUNT(*) FROM "incoming_mails" SQL (0.1ms) ROLLBACK Mar 19, 2011 at 20:44
  • errors with Failure/Error: mail.from == 'xx' NoMethodError: You have a nil object when you didn't expect it! You might have expected an instance of Array. The error occurred while evaluating nil.from Mar 19, 2011 at 20:47
  • Also tried wrapping it in a lambda, lambda {....}.should change(IncomingMail, :count).by(1) same issue, didn't update the count. Does this have something to do with the fact that The controller create's the record and that the record isn't being created inside of rspec? Mar 19, 2011 at 21:01
  • I checked the logs. While the test above is hitting the controller, it is never going inside the method def create. Y? Mar 19, 2011 at 21:26
  • I do have this in my rake routes: POST /incoming_mails(.:format) {:controller=>"incoming_mails", :action=>"create"} Mar 19, 2011 at 21:27

1 Answer 1

2

This is how i do it :

Route Example :

post  'train_ability/:ability' => :train_ability, :as => 'train_ability'

Spec :

it "should increase the strength ability by one point and also update the strength_points by one if strength is the trained ability" do
    @user.str = 10
    @user.str_points = 0
    @user.save!
    post :train_ability, :ability => 'str'
    @user.reload
    flash[:error].should be_nil
    @user.str_points.should == 1
    @user.str.should == 11
end
5
  • Thanks for the answer. This seems to be very heavy? Should I not be using rspec for this, is cucumber more appropriate? Mar 19, 2011 at 23:11
  • Also, I don't see where any of the params are being posted? Mar 19, 2011 at 23:12
  • Thanks again but I just don't see how this applies to the question? What am I missing? thanks Mar 19, 2011 at 23:20
  • Rspec is ok for this. Take a look at the post inside the spec and you will see the :ability param. Why do you think it does not apply ? I use that all the time to post in my rspec methods.
    – Spyros
    Mar 20, 2011 at 0:03
  • For some reason a simple post :create always throws ThreadError: already initialized for me :/
    – xeruf
    Nov 2, 2022 at 11:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.