簡體   English   中英

如何訪問Capybara使用的相同數據庫數據

[英]How to access the same DB data that Capybara uses

我有一個非常簡單的密碼重置表單,它只是一個用於輸入電子郵件和提交按鈕的文本字段。

有一些使用JS的客戶端驗證,因此我在為它編寫規格測試時使用Capyabara JS驅動程序。

此測試僅測試是否將密碼重置令牌添加到用戶的auth_info表中。

describe "password reset form", js: true do
  let(:email) { "foo@example.com" }

  # Create existing user with an email so we can reset it's password
  let!(:user) { create(:user, email: email) }

  before(:each) do
    fill_in email_field, with: email
    click_button reset_button
  end

  it "generates a new token" do
    # `token` is definitely getting set properly when I pause it here
    # with binding.pry and inspect the object using `user.reload`
    # But when running the test it always shows up as `nil`
    expect(user.reload.auth_info.token).to match(/[A-Fa-f0-9]{32}/)
  end
end

正如評論所指出的,我知道當我直接使用binding.pry檢查令牌時,令牌已正確設置。 但是RSpec和Capybara認為它是nil ,即使在使用reload刷新模型之后也是如此。

水豚保持着不同的緩存還是其他東西?

謝謝!

編輯:還嘗試了將reload應用於User模型以及AuthInfo模型的不同組合,以防萬一我也需要刷新后者

您正在使用具有JS功能的瀏覽器,這意味着click_button是異步的。 結果是您正在執行click_button,然后在按鈕觸發的動作發生之前立即檢查令牌。 您可以通過在expect和測試應該通過之前進入sleep 5來驗證這一點。 使測試在檢查之前等待的正確方法是使用capybaras匹配器在click_button完成后更改的頁面上查找信息,類似於以下兩種情況之一

expect(page).to have_text('text that appears after click_button has succeeded')
expect(page).to have_selector('div.abcde') #element that appears after click_button has succeeded

這些將使測試等到操作完成后,然后您才能檢查令牌

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM