如果我的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
我用方法看不到我在 capybara 输出中的最后九个帖子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
我可以在 capybara 输出中看到我的帖子列表。
如何使用模型范围测试我与 capybara 的集成测试?