title: Ruby on Rails 4 - Rspec: count should have been changed by 1, but was changed by 0
body: <p>I'm currently working on a Ruby on Rails Application and I'm new to testing. A few day ago I started to write tests using the gems <code>rspec-rails</code>, <code>factory_girl_rails</code> and <code>faker</code>.</p>
  
  <p>Writing tests for model validations was quite easy, but now I'm stuck at writing controller tests.
  I have a simple class "Message", which I created by using the scaffold command.</p>
  
  <p>SCHEMA:</p>
  
  <pre><code>create_table "messages", force: true do |t|
    t.string   "name"
    t.string   "email"
    t.string   "text"
    t.datetime "created_at"
    t.datetime "updated_at"   end
  </code></pre>
  
  <p>When I try to run the following test..</p>
  
  <p>spec/controllers/messages_controller_spec.rb:</p>
  
  <pre><code>  describe "POST create" do
      describe "with valid params" do
        it "creates a new Message" do
          expect {
            post :create, FactoryGirl.create(:message)
          }.to change(Message, :count).by(1)
        end
      end
    end
  </code></pre>
  
  <p>..I get this error message..</p>
  
  <blockquote>
    <p>MessagesController   POST create
        with valid params
          creates a new Message (FAILED - 1)</p>
    
    <p>Failures:</p>
    
    <p>1) MessagesController POST create with valid params creates a new
    Message
         Failure/Error: expect {
           count should have been changed by 1, but was changed by 0</p>
  </blockquote>
  
  <p>..by using this factories:</p>
  
  <p>spec/factories/message.rb:</p>
  
  <pre><code>require "faker"
  
  FactoryGirl.define do
    factory :message do |m|
      m.name { Faker::Name.name }
      m.email { Faker::Internet.email }
      m.text { Faker::Lorem.sentences }
    end
  end
  </code></pre>
  
  <p>I've been searching HOURS for an answer - but so far I haven't found a solution that works for me. I hope anyone out there can help me!</p>
  
  <p>Thanks!</p>
  
  <p>This is my Model: (But even if I remove all validations I get the same error message)</p>
  
  <pre><code>class Message &lt; ActiveRecord::Base
    validates :name, :email, :text,
      presence: { 
        value:  :true,
        message: "ist ein Pflichtfeld"
      }
  
      validates :name, length: { maximum: 50,
          too_long: "ist zu lang" }
  
      validates :email, length: { maximum: 50,
          too_long: "ist zu lang" }
  
      validates :text, length: { maximum: 10000,
          too_long: "ist zu lang" }
  
  end
  </code></pre>
  
  <p>This is my create action:</p>
  
  <pre><code>  def create
  
      @message = Message.new(message_params)
      respond_to do |format|
        if @message.save
          format.html { redirect_to action: 'new' }
          format.json { render action: 'show', status: :created, location: @message }
        else
          format.html { render action: 'new' }
          format.json { render json: @message.errors, status: :unprocessable_entity }
        end
      end
    end
  </code></pre>
  
  <p>Function: message_params</p>
  
  <pre><code>def message_params
    params.require(:message).permit(:name, :email, :text)
  end
  </code></pre>