Rspec 中的动态期望

Dynamic expectations in Rspec

我正在尝试通过 Rspec 期望测试动态 url 路径,如下所示:

describe 'registering a user' do
  context 'with valid data' do
    it 'confirms user registration' do
      visit '/users/new'
      fill_in 'Name here...', with: 'johnny bloggs'
      fill_in 'Your email here...', with: 'test1@test.com'
      click_button 'Notify me'
      expect(current_path).to eq '/users/@user.id'
      expect(page).to have_content "johhny"
      expect(page).not_to have_content "Sign up"
    end
  end
end

这是一个功能测试而不是模型单元测试,所以我想这种对象属性,@user.id,期望不应该在功能测试中。但是我想测试重定向到特定路径对于要检查的功能测试来说是有效的,我正在测试的功能的一个关键部分是操作重定向到特定于对象的显示页面?!

所以 a) 我应该如何正确地测试这个重定向,即新路径涉及动态属性和 b) 在这里做的正确的事情是否是测试 'dynamic attribute' 怎么可能在 rspec 测试中测试动态内容? .erb 也许?

先谢谢大家指教

您可以使用纯字符串插值:

expect(current_path).to eq "/users/#{ User.last.id }"

或者路由助手:

expect(current_path).to eq user_path(User.last)