0

我的 Selenium 测试有问题,看起来测试数据没有添加到正确的环境中,或者 Selenium 试图从错误的环境中读取。

下面的测试没有通过,:js => true但它失败了

it "allows the user to create a new test", :js => true do
    FactoryGirl.create(:user, email:'test@test.com', password:'passw0rd')

    p 'Here'
    p User.all

    visit '/' # Login view

    fill_in "email", with: 'test@test.com'
    fill_in "password", with: "passw0rd"
    click_button 'Login'

    expect(current_path).to eq '/home'
end

测试失败,因为用户没有登录,因此没有到达“/home”。我添加了打印输出以查看用户是否真的被添加到数据库中并且它是......

"Here"
#<ActiveRecord::Relation [#<User id: 1, email: "test@test.com", password: "$2a$10$JQBuNH95JWDjCTx0OH7JMuxcO.0XgAz6wtYc/2G8pps...", created_at: "2014-08-27 11:34:44", updated_at: "2014-08-27 11:34:44">]>

但是,当我将另一个打印输出添加到我的login_controller

def create
    p 'Here 2'
    p User.all
    ... # User authenitcation
end

控制台看起来像

"Here"
#<ActiveRecord::Relation [#<User id: 1, email: "test@test.com", password: "$2a$10$JQBuNH95JWDjCTx0OH7JMuxcO.0XgAz6wtYc/2G8pps...", created_at: "2014-08-27 11:34:44", updated_at: "2014-08-27 11:34:44">]>
"Here 2"
#<ActiveRecord::Relation []>

因此,由于某种原因,在进行身份验证时,在测试中添加的用户不在数据库中,这让我认为 Selenium 正在寻找不同的环境,即开发时应该查看测试?

如果有帮助,这里是我的一些spec_helper带有 database_cleaner 配置的文件

...

RSpec.configure do |config|

    config.use_transactional_fixtures = false

    config.before(:suite) do
      DatabaseCleaner.strategy = :truncation
    end

    config.before(:each) do
      DatabaseCleaner.strategy = :transaction
    end

    config.before(:each, :js => true) do
      DatabaseCleaner.strategy = :truncation
    end

    config.before(:each) do
      DatabaseCleaner.start
    end

    config.after(:each) do
      DatabaseCleaner.clean
    end

   ...
end

关于如何解决这个问题的任何想法?我不知道 Selenium 是否正在查看错误的环境或我的 database_cleaner 配置错误

4

1 回答 1

0

create 操作中的p输出并不太令人惊讶 - 我相信 JS 测试在不同的线程中运行(浏览器和服务器未同步),因此当pcreate 操作中的世界状态很可能不同时执行。您可以做的一件事是确认存在时间问题,即sleep(3)在该步骤之后立即在测试中添加一个硬等待(如 a )click_button

那有意义吗?

于 2014-08-27T18:23:33.740 回答