【问题标题】:Cucumber/RSpec testing of improbable errorsCucumber/RSpec 测试不可能的错误
【发布时间】:2010-02-08 12:15:49
【问题描述】:

我在测试以下控制器代码时遇到问题:

def publish
  if @article.publish
    flash[:notice] = "Article '#{@article.title}' was published."
  else
    # This is not tested
    flash[:error] = "Error publishing article."
  end
  redirect_to :action => :index
end

函数发布的地方如下所示:

def publish
  self.toggle!(:is_published)
end

函数toggle! 是原子的,理论上只有在数据库出现问题时才会失败(实际上我可以找到许多应该检测到错误的场景,因为有人破坏了发布方法的实现)。如何在 Cucumber 中测试出现错误时是否显示正确的消息?

【问题讨论】:

    标签: ruby-on-rails rspec cucumber testing


    【解决方案1】:

    在这里,看看这些: http://blog.flame.org/2009/11/19/how-i-test-ruby-on-rails-with-rspec-and-cucumber

    it "tells me to bugger off (not admin)" do
      login_user
      users = make_users
      get :index
      flash[:error].should match "You must be an administrator to access this page."
      response.should redirect_to(root_path)
    end
    

    希望这会有所帮助:)

    【讨论】:

      猜你喜欢
      • 2013-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多