2011-03-19 66 views
1

我想發佈到我的控制器在RSPEC,看到有什麼不對?它的失敗W/O錯誤:RSPEC如何發佈到控制器?這有什麼問題?

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 

更新時間:

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 

控制器

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 
              ) 
..... 
+0

添加了一個更新的方法。它仍然失敗。在test.log文件中,我看到正在創建的記錄,但隨後顯示:SQL(0.1ms)RELEASE SAVEPOINT active_record_1 SQL(0.3ms)SELECT COUNT(*)FROM「incoming_mails」 SQL(0.1ms)ROLLBACK – AnApprentice 2011-03-19 20:44:02

+0

錯誤與失敗/錯誤:mail.from =='xx' NoMethodError: 你有沒有對象,當你沒有想到它! 您可能預期了Array的一個實例。 評估nil時發生錯誤 – AnApprentice 2011-03-19 20:47:39

+0

也嘗試將它包裝在一個lambda,lambda {....}。應該改變(IncomingMail,:count).by(1)同樣的問題,沒有更新計數。這是否與控制器創建記錄和記錄不是在rspec內創建的事實有關? – AnApprentice 2011-03-19 21:01:56

回答

2

這是我要做的事:

路由實例:

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

規格:

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 
+0

感謝您的回答。這似乎很重?我應該不使用rspec嗎,黃瓜更合適嗎? – AnApprentice 2011-03-19 23:11:42

+0

另外,我沒有看到任何參數在哪裏發佈? – AnApprentice 2011-03-19 23:12:08

+0

再次感謝,但我只是不明白這是如何適用於這個問題?我錯過了什麼?謝謝 – AnApprentice 2011-03-19 23:20:24