views:

109

answers:

1

I've got a problem testing the following controller code:

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

Where the function publish looks like that:

def publish
  self.toggle!(:is_published)
end

Function toggle! is atomic and in theory will fail only when there is a problem with database (in practice I can find number of scenarios where the error should be detected because someone breaks the publish method implementation). How can I test in Cucumber that the correct message is shown in case of error?

+1  A: 

Here, check these out: 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

Hope this helps :)

Trevoke