我是新来的铁轨和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 endHere 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] endYou 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] endI think it must work.
更多推荐
发布评论