5

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, 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)>'
4

2 に答える 2

8

あなたのコードで:

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

Emailモデルが電子メールの配列と等しいはずである という期待を設定しています。Emailクラスです。クラスが配列と等しいと期待することはできません。allすべての電子メールは、クラスのメソッドを使用して見つけることができますEmail

2 つの配列が等しくなるように期待値を設定する必要があります。

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

このように動作するはずです。

于 2013-04-26T21:06:51.613 に答える