使用水豚运行系统测试时,如何测试持久层?

问题描述 投票:0回答:1

我正在开发一个 Rails 应用程序,用户可以在其中注册。

用户注册后,我想检查持久的用户记录是否符合某些期望,例如:名字已被保存。

    it 'registers the user with the correct first_name' do

      visit new_user_registration_path

      within '#new_user' do
        fill_in 'user[first_name]', with: 'Foo'
        fill_in 'user[last_name]', with: 'Bar'
        fill_in 'user[email]', with: 'foo@bar'
        fill_in 'user[password]', with: 'password'
        click_button
      end
      user = User.find_by(email: 'foo@bar')
      expect(user.first_name).to eq('Foo')
    end
  end

在上面的示例中,用户为零,因为注册请求尚未完成。 我考虑过不同的选择:

  1. 仅在处理系统测试时使用
    expect(page)
  2. page.document.synchronize
    并等待用户可用
  3. 根据每个示例的特殊性使用“解决方法”(例如:使用
    User.last

我的理解是我应该坚持使用选项 1,但有时用户操作会产生无法不在页面上呈现的效果。

您认为最好的解决方案是什么?

ruby-on-rails rspec capybara
1个回答
0
投票

我的做法似乎很可靠:

expect{click_button}.to change{User.count}.by(1)
user = User.find_by(email: 'foo@bar')
expect(user.first_name).to eq('Foo')
© www.soinside.com 2019 - 2024. All rights reserved.