RSpec集成测试与ajax / js表单提交无法正常工作

时间:2011-11-02 11:21:05

标签: ruby-on-rails rspec ruby-on-rails-3.1 rspec2

由于某些原因,此测试失败,这与测试必须发送电子邮件有关。它没有将电子邮件存储在数组中,我有另一个几乎相同的测试,唯一的区别是它没有使用ajax / js表单提交,并且测试通过正常,电子邮件到达数组。

有什么想法吗?

控制器:

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

错误:

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

支持/ mailer_macros

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

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

1 个答案:

答案 0 :(得分:2)

要测试js,您必须在代码中明确说明它。

See this Railscast