使用Capybara GEM单击“更新”按钮时,我的实例没有被更新[rails / rspec / testing]

发布于 2025-01-24 22:17:12 字数 1983 浏览 2 评论 0原文

为了简短但很清楚,我正在使用Rails应用程序,现在正在测试控制器。

我使用FactoryBot来生成与数据库清洁器(截断器)一起工作的实例,以清理每个测试后的数据库,而Capybara则可以测试我的控制器,从而模仿用户操作。

Factory Bot正常工作,我的实例正确生成,我可以与 /测试它们。

买是我的问题: 每当我测试控制器的“更新”方法时,我都希望在单击“保存按钮”时更新我的​​实例属性的值。但是,当我使用p标签检查值时(单击我的“保存”按钮之前和之后)什么都不会改变!

我一直在这个问题上度过一个下午,希望有人可以帮助我了解我在做什么错...

这是我的代码:

require 'rails_helper'

RSpec.describe "UserDetails", type: :controller do
  render_views

describe "PATCH /update", focus: true do
    before do
      @user = create(:user, :amin)
      @user_detail = create(:user_detail, :user_detail_user_2)
      login_as(@user)
    end

    it "should be possible to update the user details" do
      visit "/user_details/#{@user_detail.id}/edit"
      expect(response).to have_http_status(:ok)
      expect(page).to have_text("Edit your personal information")
      expect(page.current_url).to match("/user_details/#{@user_detail.id}/edit")

      p @user.user_detail.mobile_number # nil
      p @user_detail.mobile_number      # nil

      expect(@user.user_detail.mobile_number.nil?).to be true

      fill_in "user_detail[mobile_number]", with: "07 123 123"
      expect(page).to have_button("Save")
      click_link_or_button('Save')

      expect(@user.user_detail.mobile_number).to eq("07 123 123") **#Failure/Error here**
    end
  end
end

这是我遇到的失败 /错误:

Failures:

  1) UserDetails PATCH /update should be possible to update the user details
     Failure/Error: expect(@user.user_detail.mobile_number).to eq("071111111")
     
       expected: "07 123 123"
            got: nil
     
       (compared using ==)
     # ./spec/requests/user_details_controller_spec.rb:56:in `block (3 levels) in <top (required)>'

我的用户和用户和user_detail实例都有效ID。在Mobile_phone属性上没有验证,但是我仍然希望此值在单击我的“保存”按钮时会更新。

顺便说一句,我尝试使用JS:True to checkcapybara循环进行测试。一切都在正确地发生。我什至看到显示的新电话号码正在显示。但是,当我检查_spec文件中的值时,我仍然将其视为一个值。

我该如何解决?有人解释了为什么它不起作用吗?

非常感谢您!

To make it short but clear, I'm working on a Rails app and I'm now testing my controllers.

I use FactoryBot to generate instances to work with, database cleaner (truncation) to clean up my database after each test, and Capybara to test my controllers, emulating the user actions.

Factory bot is working properly and I have my instances being generated correctly and I can interact with / test them.

Buyt here's my problem:
Whenever I test the "Update" method of my controller, I expect the value of the selected attribute of my instance to be updated when clicking my "Save button". But when I use the p tag to check the value (Before and after clicking my "Save" button) nothing changed !

I've been spending the afternoon on this issue and wish someone could help me out to understand what I'm doing wrong...

Here's my code:

require 'rails_helper'

RSpec.describe "UserDetails", type: :controller do
  render_views

describe "PATCH /update", focus: true do
    before do
      @user = create(:user, :amin)
      @user_detail = create(:user_detail, :user_detail_user_2)
      login_as(@user)
    end

    it "should be possible to update the user details" do
      visit "/user_details/#{@user_detail.id}/edit"
      expect(response).to have_http_status(:ok)
      expect(page).to have_text("Edit your personal information")
      expect(page.current_url).to match("/user_details/#{@user_detail.id}/edit")

      p @user.user_detail.mobile_number # nil
      p @user_detail.mobile_number      # nil

      expect(@user.user_detail.mobile_number.nil?).to be true

      fill_in "user_detail[mobile_number]", with: "07 123 123"
      expect(page).to have_button("Save")
      click_link_or_button('Save')

      expect(@user.user_detail.mobile_number).to eq("07 123 123") **#Failure/Error here**
    end
  end
end

Here's the Failure / Error I get:

Failures:

  1) UserDetails PATCH /update should be possible to update the user details
     Failure/Error: expect(@user.user_detail.mobile_number).to eq("071111111")
     
       expected: "07 123 123"
            got: nil
     
       (compared using ==)
     # ./spec/requests/user_details_controller_spec.rb:56:in `block (3 levels) in <top (required)>'

My user and user_detail instances both are valid with an id. There's no validation on the mobile_phone attribute, but I still want this value to get updated when clicking my "Save" button.

By the way, I tried running the test with js: true to checkCapybara cycle. Everything is happening properly. I even see the view being rendered with the new phone number being displayed. But when i checked the value in the _spec file, I still get nil as a value.

How can I fix that? Does anyone has the explanation of why it's not working?

Thank you very much in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白云悠悠 2025-01-31 22:17:12

我刚刚找到了解决方案...我在这里发布它,而不是删除帖子,因为我确定它将在将来(包括我自己)帮助其他人。

在您通过Capybara单击“更新 /创建”按钮的那一刻,以及我们要测试属性的值是否更改的那一刻,我们需要使用“ your_instance.reload”。

      click_link_or_button('Save')
      @user_detail.reload
      expect(@user.user_detail.mobile_number).to eq("071111111")

现在,控制台的结果是:

UserDetails
  PATCH /update
nil
nil
"071111111"
"071111111"
    should be possible to update the user details

Finished in 5.01 seconds (files took 8.98 seconds to load)
1 example, 0 failures

字面上一个简单的词。而且没有人谈论过……我感到荒谬。

有一个了不起的一天!

I just found the solution... I post it here rather than deleting the post as I'm sure It'll help other people in the future (Including myself).

We need to use "your_instance.reload" between the moment you click the update / create button via Capybara, and the moment we want to test if the value of an attribute changed or not.

      click_link_or_button('Save')
      @user_detail.reload
      expect(@user.user_detail.mobile_number).to eq("071111111")

Now the result in the console:

UserDetails
  PATCH /update
nil
nil
"071111111"
"071111111"
    should be possible to update the user details

Finished in 5.01 seconds (files took 8.98 seconds to load)
1 example, 0 failures

Literaly one simple word. And no one ever talked about it... I feel ridiculous.

Have an amazing day guys !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文