Capybara测试在运行时失败,但手动运行相同的步骤是成功的

时间:2014-05-12 19:09:40

标签: ruby-on-rails ruby rspec capybara

所以我正在尝试为我正在处理的这个应用程序编写一些集成测试,并且我在运行后通过步骤执行并在错误页面上执行的测试之一,同时手动执行完全相同的步骤在正确的页面上。这是测试:

describe 'sign up page' do
    it 'signs a user up when valid information is entered and displays success message' do
        sign_up_with('John Doe', 'user11@gmail.com', 'a8wsk389nd9w02kdncui20dkc')
        current_path.should == root_path
        page.should have_content('Email sent with password set instructions')
    end
    ...
    def sign_up_with(name, email, registration)
        visit('/signup')
        fill_in 'Full Name', with: name
        fill_in 'Email Address', with: email
        fill_in 'Email Address Confirmation', with: email
        fill_in 'Registration Code', with: registration
        click_button('Create Java user')
    end
end

当我运行此测试时,它不会向我抛出任何错误,但是路由转到/ java_users,这是页面失败的地方,而不是/它应该如此。如果我使用相同的数据手动执行这些相同的步骤,它将完美地工作。老实说,我不知道出了什么问题,或者我做错了什么。我是Capybara(和RoR)的新手,所以它可能非常简单。我唯一可以做的就是因为ActionMailer被调用来发送一封电子邮件,不知怎的。任何帮助将不胜感激。

更新

虽然我在运行测试时没有收到任何错误(测试失败除外),但我安装了launchy gem并尝试了save_and_open_page。它带我到一个带有文字墙的页面。这是一个片段:

ArgumentError at /java_users ============================ > Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true app/views/support_mailer/password_set.text.erb, line 3 ------------------------------------------------------ ``` ruby 1 In order to set your password, please click the URL below. 2 > 3 <%= password_set_url(@user.password_reset_token) %> 4 5 If you did not purchase a *******, just ignore this email.``` App backtrace ------------- - app/views/support_mailer/password_set.text.erb:3:in `_app_views_support_mailer_password_set_text_erb___10448386_70208856' - app/mailers/support_mailer.rb:250:in `password_set' - app/models/user.rb:65:in `send_password_set' - app/controllers/java_users_controller.rb:60:in `create' - spec/features/sign_up_spec.rb:28:in `sign_up_with' - spec/features/sign_up_spec.rb:5:in `block (2 levels) in...

此处还有控制器中的create函数:

def create
    @java_user = JavaUser.new(user_params)
    registration = RegistrationCode.find_by_code(params[:registration_code])
    email = params[:java_user][:email]
    email_confirmation = params[:email_confirmation]
    user = User.find_by_email(email)

    if validateInput(registration, email, email_confirmation, user)
      @java_user.rails_uuid = user.id

      if @java_user.save
        registration.user_id = @java_user.id
        registration.save

        if user
          user.send_password_set
          redirect_to root_url, notice: "Email sent with password reset instructions"
        else
          render :new
        end
      else
        render :new
      end
    else
     render :new
    end
end

1 个答案:

答案 0 :(得分:0)

我好像已经解决了。我在/ config / environments

中的test.rb文件中缺少以下内容
config.action_mailer.default_url_options = { :host => "localhost:3000" }

我在development.rb中有它,但在test.rb中没有。