我试图在我的模型规范测试中编写几个测试,无论是否传入查询,以下方法的逻辑都有效。
models/payment.rb
include PgSearch
pg_search_scope :search,
:against => [:id, :transaction_id],
:using => {:tsearch => {:prefix => true, :dictionary => "english"}},
:associated_against => {user: [:email, :name]}
def self.text_search(query)
if query.present?
search(query)
else
scoped
end
end
这是我正在尝试编写的测试类型的示例,但对完成此操作的最佳方法却一无所知。
/spec/models/payment_spec.rb
describe '#text_search' do
it "works when query is passed in" do
payments = Payment.text_search(stub(:query))
payments.should_not be_nil
# is this even a good test??
end
it "still works if nothing is passed in" do
payments = Payment.text_search(nil)
payments.should_not be_nil
# same here, does this spec test for anything helpful??
end
end