LondonGuy
LondonGuy

Reputation: 11108

RSpec integration test not working as expected with ajax/js form submission

For some reason this test is failing and it's something to do with when the test has to send an email. It isn't storing the email in the array and I have another nearly identical test with the only difference being it doesn't use ajax/js for the form submission and that test passes fine and emails get to the array.

Any ideas?

Controller:

class UsersController < ApplicationController

  def new
    @user = User.new
  end

  def create
    @user = User.new(params[:user])     
    respond_to do |format|
      if @user.save 
        UserMailer.join_confirmation(@user).deliver
        format.js   { render :js => "window.location = '#{temp_success_path}'" }

      else

        format.html { render :new }  
        format.js   { render :form_errors }
      end
    end
  end


end

user_spec

require 'spec_helper'

describe "Users" do

  describe "signup" do

    describe "failure" do

      it "should not make a new user" do

        lambda do
          visit root_url
          fill_in "user[first_name]",             :with => ""
          fill_in "user[last_name]",              :with => ""
          fill_in "user[email]",                  :with => ""
          fill_in "user[password]",               :with => ""
          fill_in "user[username]",               :with => ""          
          click_button "join_submit"
          response.should render_template 'users/new'
          response.should have_selector :div, :id => "error_explanation"
          last_email.should be_nil
        end.should_not change User, :count

      end
    end

    describe "success" do

      it "should make a new user" do

        lambda do
          user = Factory :user
          visit root_url
          fill_in "user[first_name]",             :with => user.first_name
          fill_in "user[last_name]",              :with => user.last_name
          fill_in "user[email]",                  :with => user.email
          fill_in "user[password]",               :with => user.password
          fill_in "user[username]",               :with => user.username         
          click_button "join_submit"
          last_email.to.should include user.email
          response.should render_template :js => "window.location = '#{temp_success_path}'"

        end.should change User, :count

      end
    end
  end



end

error:

Failures:

  1) Users signup success should make a new user
     Failure/Error: last_email.to.should include user.email
     NoMethodError:
       You have a nil object when you didn't expect it!
       You might have expected an instance of Array.
       The error occurred while evaluating nil.to
     # ./spec/requests/users_spec.rb:40:in `block (5 levels) in <top (required)>'
     # ./spec/requests/users_spec.rb:31:in `block (4 levels) in <top (required)>'

Finished in 148.66 seconds
2 examples, 1 failure

Failed examples:

rspec ./spec/requests/users_spec.rb:29 # Users signup success should make a new user

support/mailer_macros

module MailerMacros
  def last_email
    ActionMailer::Base.deliveries.last
  end

  def reset_email
    ActionMailer::Base.deliveries = []
  end
end

Upvotes: 1

Views: 1194

Answers (1)

apneadiving
apneadiving

Reputation: 115541

To test js, you have to state it explicitly in your code.

See this Railscast.

Upvotes: 2

Related Questions