【问题标题】:Should and should_not in rspec 3+rspec 3+ 中的应该和不应该
【发布时间】:2015-08-15 12:22:12
【问题描述】:

将 rspec 更新为 3.3.3 后,我收到了很多弃用警告..

should我改自:

    describe "visiting the user index" do
      before {visit users_path}
      scenario{ should have_title('Sign in')}
    end

到:

    describe "visiting the user index" do
      before {visit users_path}
      scenario{ expect have_title('Sign in')}
    end

我希望我的重构没问题。

我在shuold_not 上收到了弃用警告,其中expect_not 不起作用。在网络上,我不断访问this 页面,但我无法理解should_not resplacement 中的想法。

无论如何,这个例子已被弃用:

  describe "visiting Users#edit page" do
    before { visit edit_user_path(wrong_user)}
    scenario{ should_not have_title('Edit user')}
  end

这确实得到一个未定义的方法错误:

  describe "visiting Users#edit page" do
    before { visit edit_user_path(wrong_user)}
    scenario{ expect_not have_title('Edit user')}
  end

【问题讨论】:

  • 试试expect(page).not_to have_title('Edit user')
  • rspec 返回:Capybara::RSpecMatchers::HaveTitle implements a legacy RSpec matcher protocol. For the current protocol you should expose the failure messages via the 'failure_message' and 'failure_message_when_negated' methods.
  • 是的,这是水豚的问题。您需要更新您的 Capybara 版本。

标签: ruby-on-rails ruby-on-rails-4 rspec rspec3


【解决方案1】:

如果您定义了主题,您可以使用is_expected.not_to,或者更长的版本expect(page).not_to

describe "visiting Users#edit page" do
  before { visit edit_user_path(wrong_user)}
  scenario{ is_expected.not_to have_title('Edit user')}
end

查看更好规格中的expect VS should section

【讨论】:

  • 查看我对@Abraar Arique 的评论
  • 这实际上是另一个问题。查看 capybara 问题,似乎在版本 > 2.3 时处理。你有哪个版本的水豚?可以更新吗?
  • 那么@nir,它有帮助吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多