测试中的Ruby on Rails Coursera类错误

jlarks32

我目前正在参加Coursera的免费Ruby on Rails入门课程。我正在做第三项作业,其中包括创建一个People类,该类中您具有一些功能,例如搜索功能。

rspec使用他们设计的单元测试运行时出现一个奇怪的错误我99%确信错误在于单元测试中。具体来说,在触摸任何文件之前,我都会收到以下错误:

        raise <<-EOS
        #{description} accessed in #{article} #{hook_expression} hook at:
          #{CallerFilter.first_non_rspec_line}

        `let` and `subject` declarations are not intended to be called
        in #{article} #{hook_expression} hook, as they exist to define state that
        is reset between each example, while #{hook_expression} exists to
        #{hook_intention}.
        EOS

      RuntimeError:
        let declaration `class` accessed in an `after(:context)` hook at:
          /Users/<username>/.rvm/gems/ruby-2.4.0/gems/rspec-core-3.7.1/exe/rspec:4:in `<top (required)>'

        `let` and `subject` declarations are not intended to be called
        in an `after(:context)` hook, as they exist to define state that
        is reset between each example, while `after(:context)` exists to
        cleanup state that is shared across examples in an example group.

对于初学者来说,我并不完全理解他们用来描述谈论他们的测试的语法。其次,这是Coursera类的作者编写的原始测试文件:

require 'rspec'
require 'rspec/its'
require_relative '../module2_lesson3_formative.rb'

describe "lesson3" do

  context "check results" do
    p1 = Person.new("Ivana", "Trump")
    p2 = Person.new("Eric", "Trump")
    p3 = Person.new("Melania", "Trump")
    p4 = Person.new("Marla", "Maples")

    it "unexpected search result" do
      expect(Person.search("Trump").size).to be == 3
    end
  end

  context "check instance properties" do
    subject(:john) { Person.new("Chris", "Christie") }

    it "missing first_name" do
      is_expected.to respond_to(:first_name)
    end

    it "missing last_name" do
      is_expected.to respond_to(:last_name)
    end

  end

  context "check class properties" do
    subject(:class) { Person }

    it "missing search" do
      is_expected.to respond_to(:search)
    end
  end
end

我希望有人可以在我运行时向我解释调试信息rspec我使用的RSpec 3.7是我猜测的问题,如此处所示,这可能是版本升级的问题这也可以解释一个事实,即班级的作者没有故意推销不良代码。对我来说,解决此问题的最佳方法是什么?为什么这样的行:

subject(:john) { Person.new("Chris", "Christie") }

形式不好?非常感谢!非常感谢您的时间:)

Sebastian Palma的图片

为了更改规格的主题类,您可以根据需要在每个示例中“重新定义”主题。

尝试使用let或subject更改规范主题的类时,您会收到详细的错误(警告)消息:

letsubject声明不希望在after(:context)钩子中调用,因为它们的存在是为了定义在每个示例之间重置的状态

因此,您不能显式设置主题的类,因为它将在每个运行的示例中重置。

您可以使用just将主题设置为“检查类属性”上下文中的Person对象subject,这种方式is_expected将检入响应类方法搜索的对象,例如:

context "check class properties" do
  subject { Person }

  it 'missing search' do
    is_expected.to respond_to(:search)
  end
end

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章