RSPEC 如何发布到控制器?这有什么问题吗?

发布于 2024-10-23 01:28:16 字数 1644 浏览 2 评论 0原文

我正在尝试在 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
                                            )
.....

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

转身泪倾城 2024-10-30 01:28:16

我就是这样做的:

路线示例:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文