2013-04-29 24 views
1

post_controller_spec.rb故障/錯誤:受讓人(:文章)。應該EQ([文章])

require 'spec_helper' 
describe Blog::PostsController do 
    let!(:posts) { FactoryGirl.create_list(:post, 3) } 
    let!(:post) { posts.first } 
    describe "GET index" do 
    it "renders the :index view" do 
     get :index 
     assigns(:posts).should eq([post]) 
     response.should render_template :index 
    end 
    end 
describe "GET show" do 
    context "invalid post" do 
    before do 
     get :show, :id => 99 
    end 
    it "redirects to the 404 page" do 
    response.should render_template(:file => "#{Rails.root}/public/404.html") 
    end 
    end 
    context "valid post" do 
    it "show page" do 
     get :show, id: post 
     assigns(:post).should eq(post) 
     response.should render_template :show 
     end 
    end 
    end 
end 

這是我的索引行動:

def index 
    @posts = Post.all.order_by(created_at: :desc).page(params[:page]) 
    respond_to do |format| 
     format.html 
    end 
    end 

我得到這個錯誤:

1) Blog::PostsController GET index renders the :index view 
Failure/Error: assigns(:posts).should eq([post]) 

    expected: [#<Post _id: 517ebb98a616542f41000002, created_at: 2013-04-29 18:27:36 UTC, updated_at: 2013-04-29 18:27:36 UTC, image_filename: nil, impressions_count: nil, tags: ["tag1", "tag2", "tag3", "tag4"], _slugs: ["post-number-1"], title: "Post number 1", content: "[\"Maiores dolor illum distinctio eveniet perspiciatis necessitatibus consequatur. Dicta ratione repellat ullam sit sed inventore voluptatem. Possimus magni cum dolores rerum voluptas quibusdam. Sed rerum atque accusantium amet aut.\", \"Consequatur ab eum voluptatem voluptatem sit et. Natus soluta quam sed quasi vel odio assumenda. Nulla excepturi dicta voluptatem voluptas vel sit.\"]", image: nil, image_cache: nil, remove_image: nil, admin_id: "517ebb98a616542f41000001">] 
     got: #<Mongoid::Criteria 
    selector: {} 
    options: {:sort=>{"created_at"=>-1}, :limit=>9, :skip=>0} 
    class: Post 
    embedded: false> 


    (compared using ==) 

    Diff: 
    @@ -1,2 +1,6 @@ 
    -[#<Post _id: 517ebb98a616542f41000002, created_at: 2013-04-29 18:27:36 UTC, updated_at: 2013-04-29 18:27:36 UTC, image_filename: nil, impressions_count: nil, tags: ["tag1", "tag2", "tag3", "tag4"], _slugs: ["post-number-1"], title: "Post number 1", content: "[\"Maiores dolor illum distinctio eveniet perspiciatis necessitatibus consequatur. Dicta ratione repellat ullam sit sed inventore voluptatem. Possimus magni cum dolores rerum voluptas quibusdam. Sed rerum atque accusantium amet aut.\", \"Consequatur ab eum voluptatem voluptatem sit et. Natus soluta quam sed quasi vel odio assumenda. Nulla excepturi dicta voluptatem voluptas vel sit.\"]", image: nil, image_cache: nil, remove_image: nil, admin_id: "517ebb98a616542f41000001">] 
    +#<Mongoid::Criteria 
    + selector: {} 
    + options: {:sort=>{"created_at"=>-1}, :limit=>9, :skip=>0} 
    + class: Post 
    + embedded: false> 

# ./spec/controllers/blog/posts_controller_spec.rb:8:in `block (3 levels) in <top (required)>' 

我的表演動作,通過測試。我在哪裏有索引操作的錯誤?

回答

0

Mongoid不取數據,直到它絕對有。所以這意味着,當你做Model.where時,你會得到一個Mongoid::Criteria對象。這個類實現了counteach(也包括Enumerable),所以它通常表現得像Array,但事實並非如此。您需要使用to_a進行轉換。

這應該解決的錯誤:

describe "GET index" do 
    it "renders the :index view" do 
    get :index 
    assigns(:posts).to_a.should match_array posts 
    response.should render_template :index 
    end 
end 

你的表演動作成功運作的原因,是Model.find,並Model.find_by會返回一個對象,或者如果對象不存在引發異常;所以Mongoid立即運行查詢。

+0

謝謝,現在,我得到一個額外的元素的錯誤:'額外的元素是:[# hyperrjas 2013-04-30 08:37:36

+0

啊對,我看你有FactoryGirl創建3個帖子。這就說得通了。更新了我的答案。 – 2013-04-30 12:41:17

+0

謝謝你的回覆工作正常:)。問候! – hyperrjas 2013-04-30 13:26:43

0

這是因爲你試圖比較不同類型的對象。

你可以嘗試轉換assings[:posts]這樣的:

assings[:posts].to_a.should eq([post]) 

但由於陣列是不一樣的,我認爲它不會太工作。

所以,你可以嘗試以驗證是否post裏面assings[:post]

assings[:posts].should include(post) 
+0

謝謝,但用'assignments [:posts] .should include(post)',我得到相同的錯誤 – hyperrjas 2013-04-29 21:10:46