Oh! I'm an idiot. It's not mixing in the ControllerMixin that generates current_foo, it's spinning up the app. Perhaps if you changed the point at which current_XXX is generated from app load time to ControllerMixin include/extend/whatever time you could test it as I described.<div>
<br></div><div>But that's plainly more work.</div><div><br></div><div>Sorry. Ignore me. Delete this message. Erase your history.</div><div><br><div class="gmail_quote">On 4 October 2011 13:33, Murray Steele <span dir="ltr"><<a href="mailto:murray.steele@gmail.com">murray.steele@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">Can't you test the ControllerMixin directly?<div><br></div><div>something like (written in email client, apologies for all the wrongness):</div>
<div><br></div><div>class ControllerMixinTest < ActiveSupport::TestCase</div>
<div><br></div><div> class MyTest; end</div><div><br></div><div> test "the controller mixin supplies a current_foo when MyEngine.model is 'foo'" do</div><div> MyEngine.model = 'foo'</div><div>
<br></div><div> assert !MyTest.new.responds_to?(:current_foo)</div><div><br></div><div> MyTest.include ControllerMixin # or however it's supposed to be mixed in</div><div><br></div><div> assert MyTest.new.responds_to?(:current_foo)</div>
<div> end</div><div><br></div><div> # Then continue on to test what this magical current_foo method does.</div><div>end<div><div></div><div class="h5"><br><br><div class="gmail_quote">On 4 October 2011 13:27, Andrew Stewart <span dir="ltr"><<a href="mailto:boss@airbladesoftware.com" target="_blank">boss@airbladesoftware.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hola El Rug,<br>
<br>
I have a Rails 3 engine which mixes a module into ActionController::Base. The module provides helper methods in the controller such as `current_foo`.<br>
<br>
Now, however, I want to parameterise those methods based on a configuration value. So if somebody has a Bar model instead of a Foo model, the controller will get a `current_bar` method.<br>
<br>
I think I have a working solution but I can't figure out how to test it. Here's what I've got:<br>
<br>
module ControllerMixin<br>
# original hard-coded method:<br>
# def current_foo<br>
# @current_foo ||= Foo.find(session[:current_foo_id]) if session[:current_foo_id]<br>
# end<br>
<br>
# parameterisable method:<br>
class_eval <<-END<br>
def current_#{MyEngine.model} ||= #{MyEngine.model.classify.constantize}.find(session[:current_#{...}_id]) if ...<br>
END<br>
end<br>
<br>
-- where MyEngine.model is set to 'foo' or 'bar' or whatever in an initializer (defaults to 'foo').<br>
<br>
This appears to work when I use it in a Rails app, but I can't test the 'bar' case in the engine's gem because the Rails app has already initialised with the default value 'foo' before my test's setup method can set it to 'bar'. I.e.:<br>
<br>
class BarTest < ActiveSupport::TestCase<br>
setup do<br>
MyEngine.model = 'bar' # too late! ActionController::Base already has default `current_foo` mixed in<br>
end<br>
<br>
test 'current_bar correctly defined' do<br>
... # fails because `current_foo` exists, not `current_bar`<br>
end<br>
end<br>
<br>
Any ideas? Maybe a lazy-loading alternative to the class_eval approach or somehow setting MyEnding.model before Rails spins up...?<br>
<br>
Cheers,<br>
Andy Stewart<br>
<br>
-------<br>
<a href="http://airbladesoftware.com" target="_blank">http://airbladesoftware.com</a><br>
_______________________________________________<br>
Chat mailing list<br>
<a href="mailto:Chat@lists.lrug.org" target="_blank">Chat@lists.lrug.org</a><br>
<a href="http://lists.lrug.org/listinfo.cgi/chat-lrug.org" target="_blank">http://lists.lrug.org/listinfo.cgi/chat-lrug.org</a><br>
</blockquote></div><br></div></div></div>
</blockquote></div><br></div>