在安装Rails之前测试运行

时间:2012-10-09 16:43:22

标签: ruby-on-rails integration-testing capybara factory-bot

我正在使用Capybara进行一些集成测试,我在运行测试时遇到了问题。看来我的设置在测试之前没有运行,或者至少是第二次运行的测试。以下是我到目前为止的测试:

require 'test_helper'

class UserSignupTest < ActionDispatch::IntegrationTest

    setup do
        FactoryGirl.create(:user_role)
    end

    test "user organic signup" do
        visit new_user_registration_path
        assert page.has_content?('Sign up for Connectedtrips')

        fill_in('Email', :with => "hugo@gmail.com")
        fill_in('First name', :with => "Hugo")
        fill_in('Last name', :with => "KH")
        choose("is_teacher_no")
        choose("owns_center_no")
        fill_in("Password", :with => "password")
        fill_in("Password confirmation", :with => "password")
        click_button("Sign up")

        assert page.has_content?("Your registration is complete - thanks for joining!")
    end

    test "teacher organic signup" do
        visit new_user_registration_path
        assert page.has_content?('Sign up for Connectedtrips')
        fill_in('Email', :with => "another@gmail.com")
        fill_in('First name', :with => "Another")
        fill_in('Last name', :with => "")
        choose("is_teacher_yes")
        choose("owns_center_no")
        fill_in("Password", :with => "password")
        fill_in("Password confirmation", :with => "password")
        click_button("Sign up")

        assert page.has_content?("Your registration is complete - thanks for joining!")
    end
end

这是我的测试助手中的代码。我试着尽可能地设置它,我不确定我是否需要数据库更清洁,因为我使用Datamapper而不是Active Record,因为我不知道这是否会产生影响。

ENV [“RAILS_ENV”] =“测试” 需要File.expand_path('../../ config / environment', FILE ) 需要'rails / test_help' 要求'capybara / rails' 要求'database_cleaner'

module ActionController
  class IntegrationTest
    include Capybara::DSL
    DataMapper.auto_migrate!

    def teardown
      DatabaseCleaner.clean 
      Capybara.reset_sessions!
      Capybara.use_default_driver
      destroy_all
    end

    def destroy_all
        # destroying all my tables here 
      end

  end
end

这是我收到的错误:

UserSignupTest
     PASS test_teacher_organic_signup (0:00:00.929)
    ERROR test_user_organic_signup (0:00:00.998)
          ERROR:  insert or update on table "users" violates foreign key constraint "users_role_fk"
          DETAIL:  Key (role_id)=(1) is not present in table "roles".

我不太清楚如何解决这个问题,我将非常感谢你的帮助。

1 个答案:

答案 0 :(得分:0)

我解决了这个问题,并认为我只是提出了我的解决方案。我所做的就是在我拆除所有桌子的拆机中,我遗漏了角色表被摧毁。我做的设置:

Role.first(:name => "User") || FactoryGirl.create(:user_role)

这可能有点矫枉过正。无论如何我的问题解决了。