如何为活动记录范围编写测试?
How to write tests for active record scopes?
如何为活动记录范围编写测试?例如
class Post < ActiveRecord::Base
scope :recent, -> { order("posts.created_at DESC") }
scope :published, -> { where("status = 1") }
end
我正在使用 Rspec 进行测试
RSpec.feature Post, :type => :model do
let(:post) { build(:post) }
describe 'test scopes' do
end
end
假设您有适当的固定装置设置,我通常 运行 一个查询,其中我希望从范围中获得结果,而我没有。例如:
describe '#published' do
it "returns a published post" do
expect(Post.published.count).to be(1)
# or inspect to see if it's published, but that's a bit redundant
end
it "does not return unpublished posts" do
expect(Post.published).to_not include(Post.where("status = 0"))
end
end
如何为活动记录范围编写测试?例如
class Post < ActiveRecord::Base
scope :recent, -> { order("posts.created_at DESC") }
scope :published, -> { where("status = 1") }
end
我正在使用 Rspec 进行测试
RSpec.feature Post, :type => :model do
let(:post) { build(:post) }
describe 'test scopes' do
end
end
假设您有适当的固定装置设置,我通常 运行 一个查询,其中我希望从范围中获得结果,而我没有。例如:
describe '#published' do
it "returns a published post" do
expect(Post.published.count).to be(1)
# or inspect to see if it's published, but that's a bit redundant
end
it "does not return unpublished posts" do
expect(Post.published).to_not include(Post.where("status = 0"))
end
end