ruby-on-rails – 使用RSpec测试模型中记录的正确顺序

我是rails和RSpec的新手,想要了解如何使这个测试工作的一些指示.

我希望电子邮件从最新到最旧排序,我在测试时遇到问题.

我是Rails的新手,到目前为止,我正在努力让我的测试工作,然后实际的功能.

更新

require 'spec_helper'

describe Email do

  before do
    @email = Email.new(email_address: "user@example.com")
  end

  subject { @email }

  it { should respond_to(:email_address) }
  it { should respond_to(:newsletter) }

  it { should be_valid }

  describe "order" do 

    @email_newest = Email.new(email_address: "newest@example.com")

    it "should have the right emails in the right order" do
      Email.all.should == [@email_newest,@email]
    end

  end 

end

这是我得到的错误

1) Email order should have the right emails in the right order
  Failure/Error: Email.all.should == [@email_newest,@email]
   expected: [nil,#<Email id: nil,email_address: "user@example.com",newsletter: nil,created_at: nil,updated_at: nil>]
        got: [] (using ==)
   Diff:
   @@ -1,3 +1,2 @@
   -[nil,- #<Email id: nil,updated_at: nil>]
   +[]
 # ./spec/models/email_spec.rb:32:in `block (3 levels) in <top (required)>'

解决方法

在你的代码

it "should have the right emails in the right order" do
  Email.should == [@email_newest,@email]
end

您期望电子邮件模型应该等于电子邮件数组.
电子邮件一个类.您不能只将类匹配为数组.通过使用电子邮件类中的所有方法可以找到所有电子邮件.

您必须将两个数组的期望值设置为相等.

it "should have the right emails in the right order" do
  Email.order('created_at desc').all.should == [@email_newest,@email]
end

我认为它必须奏效.

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...