RSpec 集成测试未按预期使用 ajax/js 表单提交进行工作

发布于 2024-12-13 09:33:35 字数 3097 浏览 1 评论 0原文

由于某种原因,此测试失败了,这与测试必须发送电子邮件有关。它没有将电子邮件存储在数组中,我还有另一个几乎相同的测试,唯一的区别是它不使用 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

support/mailer_macros

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一个人的旅程 2024-12-20 09:33:35

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

查看此 Railscast

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

See this Railscast.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文