【问题标题】:RSpec puts different from should equal valueRSpec put 不同于应该相等的值
【发布时间】:2011-12-12 02:12:48
【问题描述】:

我不明白为什么下面的代码块没有通过,即使 puts 值显示了我正在检查的值:

  let(:new_on_each_example) { MovieList.new }
  it "can use method defined by 'let'" do
    new_on_each_example.should_not be_nil
    # the object is memoized, so
    new_on_each_example.should == new_on_each_example

    puts new_on_each_example.class
    new_on_each_example.class.should == "MovieList"
  end

控制台显示:

电影列表

预期:“电影列表” 得到:MovieList(id: integer, title: string, created_at: datetime, updated_at: datetime, track_number: integer)(使用==)

Puts 显示我正在寻找但测试失败的值。这是为什么呢?

【问题讨论】:

    标签: ruby-on-rails ruby rspec


    【解决方案1】:
    new_on_each_example.should be_instance_of(MovieList)
    

    更多信息在rspec matchers doc

    【讨论】:

      【解决方案2】:

      您正在传递给 RSpec "MovieList"。这是一个字符串,而不是您班级的常量。所以你应该这样做:

      new_on_each_example.class.should == MovieList
      

      但是使用 RSpec 你也可以做一些更优雅的事情:

      new_on_each_example.class.should be_a(MovieList)
      

      【讨论】:

        猜你喜欢
        • 2011-12-12
        • 1970-01-01
        • 1970-01-01
        • 2016-03-28
        • 1970-01-01
        • 2017-12-06
        • 2018-02-12
        • 2022-01-14
        • 1970-01-01
        相关资源
        最近更新 更多