我是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
我认为它必须奏效.