微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我认为它必须奏效.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐