title: Create controller tests for Mongoid embedded models
body: <p>I'm working on a Rails 3 app that allows users to follow movies by adding them to their queue, so that they're notified when the movies are premiering.</p>
  
  <p>Since I'm using Mongoid, I've structured it so that when a user adds a movie to their queue, they create an embedded document, <code>adds</code>, and they also increment an <code>add_counter</code> on the <code>Movie</code> model by 1. </p>
  
  <p>I've been able to get my specs to pass confirming that this behavior is working in the model level, but now that I'm building the controller, I can't seem to get a passing response. I've been looking everywhere for a solution and even after sleeping on it and coming back to it I haven't been able to figure out what I'm doing wrong.</p>
  
  <p>Here is my controller spec:</p>
  
  <pre><code># spec/controllers/adds_controller_spec.rb
  
  require 'spec_helper'
  
  describe AddsController do
  
    before(:each) do
      @user = Factory(:user)
      @movie = Factory(:movie)
    end
  
    describe '#create' do
  
      context 'given valid params' do
  
        before(:each) do
          sign_in @user
        end
  
        it "creates a new add" do
          add_count @user.adds.count
          post :create, :movie_id =&gt; @movie.id
          @user.adds.count.should == add_count + 1
        end
  
      end
    end
  end
  </code></pre>
  
  <p>Here's my controller:</p>
  
  <pre><code># controllers/adds_controller.rb    
  
  class AddsController &lt; ApplicationController
    before_filter :authenticate_user!
  
    def create
      @movie = Movie.find(params[:movie_id])
      @user = current_user
      @user.add_to_queue!(@movie)
  
      respond_to do |format|
        format.js
      end
    end
  end
  </code></pre>
  
  <p>And here are my models:</p>
  
  <pre><code># models/movie.rb
  
  class Movie
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::MultiParameterAttributes
  
  field :add_counter, :type =&gt; Integer, :default =&gt; 0
  
  # models/user.rb
  
  class User
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::MultiParameterAttributes
  
  embeds_many :adds
  
  def add_to_queue!(movie)
    if already_added?(movie) == false
      adds.create!(:movie_id =&gt; movie.id)
      movie.add_counter += 1
    end
  end
  
  def already_added?(movie)
    adds.where(:movie_id =&gt; movie.id).exists?
  end
  
  # models/add.rb
  
  class Add
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::MultiParameterAttributes
  
  field :movie_id, :type =&gt; String
  key   :movie_id
  
  embedded_in :user
  </code></pre>
  
  <p>Finally, here's the feedback from Rspec when I run the controller test:</p>
  
  <pre><code>Failures:
  
    1) AddsController#create given valid params creates a new add
       Failure/Error: @user.adds.count.should == 1
         expected: 1
              got: 0 (using ==)
       # ./spec/controllers/adds_controller_spec.rb:25:in `block (4 levels) in &lt;top (required)&gt;'
  </code></pre>