加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-17 02:45:15 所属栏目:百科 来源:网络整理
导读:我是rails和RSpec的新手,想要了解如何使这个测试工作的一些指示. 我希望电子邮件从最新到最旧排序,我在测试时遇到问题. 我是Rails的新手,到目前为止,我正在努力让我的测试工作,然后实际的功能. 更新 require 'spec_helper'describe Email do before do @emai
我是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

我认为它必须奏效.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读