0

Postモデルにスコープがある場合、投稿リストが表示されません。

モデル:

class Post
  include Mongoid::Document
  include Mongoid::Timestamps
  ...
  scope :last_nine, where(:published.ne => nil).limit(9).order_by([:created_at, :desc])
end

コントローラ:

def index
 @posts = Post.last_nine.where(locale: locale)
  respond_to do |format|
    format.html
  end
end

カピバラとの統合テスト:

require 'spec_helper'
describe "Posts" do
  let!(:posts) { FactoryGirl.create_list(:post, 3) }
  subject { page }
  describe "index page should have last nine results" do
    posts.each do |p|
      current_path.should == root_path
      should have_selector "h4 a", :text => post.title
    end
    save_and_open_page
  end
end

メソッドを使用したカピバラの出力で、最後の 9 件の投稿が表示されませんsave_and_open_page

ただし、コントローラーを削除@posts = Post.last_nine.where(locale: locale)して追加すると、次のようになります。@posts = Post.all

def index
   @posts = Post.all
    respond_to do |format|
      format.html
    end
  end

私のテストは正常に機能して@posts = Post.allおり、カピバラの出力で投稿リストを確認できます。

カピバラとの統合テストをモデルスコープでテストするにはどうすればよいですか?

4

0 に答える 0