On Tue, Nov 4, 2008 at 1:38 PM, Fernando P. [email protected]
wrote:
it “should redirect to login path” do @site = Site.find_by_domain_name(website_name)
I thought I could stub out the find_by_domain_name method to return a
cooperative object with a traffic_available value, but it didn’t work.
How can I do that correctly?
I have found a dirty workaround:
controller.stub!(:find_current_site).and_return(:true)
but I don’t find it acceptable. I am not going to stub out all methods
in my app, that’s nonsense.
I don’t know if this will satisfy you or not, but you could write
that once and have it apply across your entire suite. Add this to
spec/spec_helper.rb (or equivalent):
Spec::Runner.configure do |config|
config.before(:each, :type => :controller) do
controller.stub!(:find_current_site).and_return(:true)
end
end
Are the docs on mock outdated? I sometimes see mock_model, and
sometimes
mock. Which one should be used?
For mock_model, you need to look at the rspec-rails gem, which is a
separate library.
You actually have three choices when mocking an ActiveRecord model:
mock, mock_model and stub_model. mock() belongs to rspec’s core
library, while the latter two come with the rails-specific extension
gem.