使用RSpec测试模型中记录的正确顺序(Using RSpec to test for correct order of records in a model)

编程入门 行业动态 更新时间:2024-10-25 10:21:12
使用RSpec测试模型中记录的正确顺序(Using RSpec to test for correct order of records in a model)

我是新来的铁轨和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, email_address: "user@example.com", newsletter: nil, created_at: nil, updated_at: nil>] +[] # ./spec/models/email_spec.rb:32:in `block (3 levels) in <top (required)>'

I'm new to rails and RSpec and would like some pointers on how to get this test to work.

I want emails to be sorted from newest to oldest and I'm having trouble testing this.

I'm new to Rails and so far I'm having a harder time getting my tests to work then the actual functionality.

Updated

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

Here is the error I get:

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, email_address: "user@example.com", newsletter: nil, created_at: 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

您正在设定Email模型应该与电子邮件数组相等的期望。 Email是一个类。 你不能只将类匹配到数组。 所有电子邮件都可以通过使用Email类中的all方法找到。

您必须设置两个数组的期望值相等。

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

我认为它必须奏效。

In your code

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

You are setting expectation that Email model should be equal to the array of emails. Email is a class. You can't just match class to be equal to an array. All emails can be found by using all method on class Email.

You must set expectation of two arrays to be equal.

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

I think it must work.

更多推荐

本文发布于:2023-04-29 12:06:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1336407.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:顺序   模型   正确   测试   RSpec

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!