简体   繁体   English

RSpec与应该相等的值不同

[英]RSpec puts different from should equal value

I cannot understand why the block of code below is not passing even though the puts value is showing the value I am checking for: 我无法理解为什么下面的代码块没有通过,即使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

The console shows: 控制台显示:

MovieList MovieList

expected: "MovieList" got: MovieList(id: integer, title: string, created_at: datetime, updated_at: datetime, track_number: integer) (using ==) 预期:“MovieList”得到:MovieList(id:整数,标题:字符串,created_at:datetime,updated_at:datetime,track_number:整数)(使用==)

Puts displays the value that I'm looking for but the test fails. Puts显示我正在寻找的值,但测试失败。 Why is this? 为什么是这样?

new_on_each_example.should be_instance_of(MovieList)

更多信息在rspec matchers doc中

You are passing to RSpec "MovieList" . 您将转到RSpec "MovieList" It's a string, not the constant for your class. 这是一个字符串,而不是你班级的常数。 So you should do something like: 所以你应该这样做:

new_on_each_example.class.should == MovieList

but using RSpec you can do something more elegant too: 但是使用RSpec你也可以做一些更优雅的事情:

new_on_each_example.class.should be_a(MovieList)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM