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

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/

10-13 08:00