Rspec的动态期望

时间:2015-05-24 23:52:32

标签: ruby-on-rails ruby testing rspec tdd

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

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)这里正确的事情是否正在测试“动态属性”如何测试动态内容在rspec测试中? 。也许吧?

提前感谢您的启蒙。

1 个答案:

答案 0 :(得分:2)

您可以使用普通字符串插值:

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

或路线助手:

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