【问题标题】:problems passing test with activeRecord, using Rails使用 Rails 通过 activeRecord 测试时出现问题
【发布时间】:2014-08-07 09:29:47
【问题描述】:

我正在测试一种方法,要求完全匹配,但测试没有通过?知道我做错了什么吗?

谢谢!!

我的方法:

 def self.search_by_number(number)
   return [] if number.blank?
   where("title LIKE ?", "%#{number}%")

 end

我的规格:

  it 'returns an array of one video  for an exact match' do
      t1 = Video.create(title: 2345)
      t2 = Video.create(title: 2323)
      expect(Video.search_by_number(2345)).to eq([t1])
    end

我在这里真的找不到问题,这是我得到的日志错误

1) Video search_by_number  returns an array of one video  for an exact match
     Failure/Error: expect(Video.search_by_number(2345)).to eq([t1])

       expected: [#<Video id: 3, title: 2345, description: nil, thumb_url: nil, large_url: nil, start_point: nil, start_time: nil, end_point: nil, end_time: nil, created_at: "2014-08-07 09:15:13", updated_at: "2014-08-07 09:15:13">]
            got: #<ActiveRecord::Relation [#<Video id: 1, title: 2345, description: nil, thumb_url: nil, large_url: nil, start_point: nil, start_time: nil, end_point: nil, end_time: nil, created_at: "2014-08-07 09:09:55", updated_at: "2014-08-07 09:09:55">, #<Video id: 3, title: 2345, description: nil, thumb_url: nil, large_url: nil, start_point: nil, start_time: nil, end_point: nil, end_time: nil, created_at: "2014-08-07 09:15:13", updated_at: "2014-08-07 09:15:13">]>

【问题讨论】:

  • 一个是Video 对象,另一个是ActiveRecordRelation 这就是它失败的原因。
  • @japed 你好!我该怎么做才能改变这个输出?
  • 如果您使用的是 rails 4,请在搜索末尾附加 .to_aexpect(Video.search_by_number(2345).to_a).to eq([t1])
  • @jvnill 你好!这是 Rails 4 的新事物吗,因为我以前做过这个? :)
  • @jvnill 也不起作用

标签: ruby-on-rails rspec


【解决方案1】:

我认为问题在于我需要安装 database_cleaner。我必须指出,我运行 Rails 4.1.1 和 dat 也可能是冲突,不知道。

我的测试数据库在测试后没有被清空。

我必须在规范文件夹中进行一些更改!

请看一下这个链接:

http://devblog.avdi.org/2012/08/31/configuring-database_cleaner-with-rails-rspec-capybara-and-selenium/

一旦我进行了这些更改,一切都运行良好!

我的代码从一开始就很好! ^_^) 不过感谢您的宝贵时间!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-31
    • 1970-01-01
    • 2012-03-22
    • 2016-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多