我是測試Rails Web應用程序和RSpec的新手。我使用遺留代碼並需要添加測試。那麼用RSpec測試查找器和命名範圍的最好方法是什麼?Rails:使用RSpec測試命名範圍
我在Google上找到了一些方法,但它們並不理想。 例如:
http://paulsturgess.co.uk/articles/show/93-using-rspec-to-test-a-named_scope-in-ruby-on-rails
it "excludes users that are not active" do
@user = Factory(:user, :active => false)
User.active.should_not include(@user)
end
或
http://h1labs.com/notebook/2008/8/21/testing-named-scope-with-rspec
it "should have a published named scope that returns ..." do
Post.published.proxy_options.should == {:conditions => {:published => true}}
end
我覺得最好的辦法(恕我直言)在 「軌道測試方」:
should_match_find_method :active_only { :active == true }
其中should_match_find_method
定製幫手方法
範圍測試是真正的經驗,我堅持你在這裏公開的第一種方法 – apneadiving
我同意。第一種方法有什麼問題?它規定了真正的行爲,而不是檢查(實際上只是重寫)可能有缺陷的配置參數。 –
@RobDavis「可能有缺陷的配置參數」+1 –