Ruby-on-rails – Rails RSpec: Controller Testing, checking if errors Array of model is filled with entries if new record cannot be created due to validation error

rspecruby-on-rails

I have a still pretty simple Rails application that I want to develop using BDD with Cucumber and TDD with RSpec. Currently, I am hanging at a test where I want to check that if a new instance of an Organizer (that's the model I have) cannot be created due to a validation error. I would like to check that the errors Array of the object to be created is not empty so that I can be sure that error messages are available for showing them in the view.

require 'spec_helper'

describe OrganizersController do
render_views

describe "POST 'create'" do

  describe "with invalid arguments" do
    before(:each) do
      request.env["HTTP_REFERER"] = organizers_new_path
      @organizer_args = { :name => "" }
    end      

    it "should return a non-empty list of errors" do
      post 'create', :organizer => @organizer_args
      @organizer.errors.empty?.should_not be_true
    end
  end
end      

end

I am developing based on Rails 3.2.9 with RSpec 2 and cucumber-rails.

Any suggestions are appreciated. Thanks!

Best Answer

You should use assigns method to get instance variable from controller action:

assigns(:organizer).errors.empty?.should_not be_true