ruby-on-rails - Cucumber/RSpec 测试不可能的错误

标签 ruby-on-rails rspec cucumber testing

我在测试以下 Controller 代码时遇到问题:

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 中测试在出现错误时是否显示正确的消息?

最佳答案

在这里,检查这些: 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

希望这有帮助:)

关于ruby-on-rails - Cucumber/RSpec 测试不可能的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2221441/

相关文章:

ruby-on-rails - 在我的 rspec 中为 #<RSpec::ExampleGroups 获取未定义的方法 `get'

ruby-on-rails - rails 上的 ruby : before_validation

java - 将控制台日志添加到 allure 报告中的每个步骤?(Java、Selenium、TestNG、Cucumber)

ruby-on-rails - rails : Cucumber + Capybara - How to add http method in visit ()

ruby-on-rails - 如何使用默认的 app/policies/application_policy.rb?

ruby-on-rails - 嵌套模块和方法 Hook

ruby-on-rails-3 - Rails FactoryGirl 复制工厂

ruby-on-rails-3 - Rails3 + Rspec2 : undefined method `authenticate!' for nil:NilClass

ruby-on-rails - rspec Rails 模拟 session 哈希

ruby-on-rails - 更改 capybara 驱动程序而不重新启动 spork