【问题标题】:Dynamic expectations in RspecRspec 中的动态期望
【发布时间】:2015-05-24 23:52:31
【问题描述】:

我正在尝试通过 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)这里要做的正确事情是否是测试“动态属性”如何测试动态内容在 rspec 测试中? .erb 也许?

提前感谢您的启发。

【问题讨论】:

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


    【解决方案1】:

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

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

    或路线助手:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-08
      • 1970-01-01
      • 2014-05-13
      • 1970-01-01
      相关资源
      最近更新 更多