Re: [Catalyst] Mocking for Catalyst testing
On 2010-01-16, at 10:31 PM, Tomas Doran wrote: I tend to just use Moose directly to construct mock classes for me: Things are improving, t0m. I tried use Class::MOP; use Class::MOP::Class; my $meta_refreshes = Class::MOP::Class-create('ARMAdmin::Model::Refreshes'); $meta_refreshes-add_method('get_profiles' = sub { die(Failed to get profiles); }); Then I start the app in the test framework. I'm just after getting the get_profiles mocked method called here, later I want it to return values I can test in the generated page. However, Catalyst then calls: Catalyst::Utils::ensure_class_loaded( $component, { ignore_loaded = 1 } ); The ignore_loaded seems to bypass everything and overwrite my newly mocked class just when I need it. I get the method redefined, and a warning about it. The comment in the code here says this is needed for some Schema::Loader functionality dependent on loading even when the class exists. Doesn't this make mocking impossible? The ignore_loaded = 1 setting seems a somewhat heavy assumption to make for all components. Can anyone offer a way around this that doesn't involve too much patching? All the best Stuart ___ List: Catalyst@lists.scsys.co.uk Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/ Dev site: http://dev.catalyst.perl.org/
Re: [Catalyst] Mocking for Catalyst testing
On 17 Jan 2010, at 18:36, Stuart Watt wrote: On 2010-01-16, at 10:31 PM, Tomas Doran wrote: I tend to just use Moose directly to construct mock classes for me: Things are improving, t0m. I tried use Class::MOP; use Class::MOP::Class; my $meta_refreshes = Class::MOP::Class- create('ARMAdmin::Model::Refreshes'); $meta_refreshes-add_method('get_profiles' = sub { die(Failed to get profiles); }); Then I start the app in the test framework. I'm just after getting the get_profiles mocked method called here, later I want it to return values I can test in the generated page. However, Catalyst then calls: Catalyst::Utils::ensure_class_loaded( $component, { ignore_loaded = 1 } ); Right, hang on.. Why are you mocking things and then loading the entire application? Usually you either run the full app for system testing (with test config that points at a test DB or whatever), or you mock _lots of stuff_ (like the context class, the request and response) and unit test one component.. Trying to mangle methods then load the full app won't work.. If you reay need to do this - use Catalyst::Test 'MyApp', then manipulate the class after catalyst has loaded it (e.g my $meta = MyApp::Model::Foo-meta; $meta-remove_method('bar'); $meta- add_method('bar' = sub { die(New bar) }); ) but I'd not recommend it.. Cheers t0m ___ List: Catalyst@lists.scsys.co.uk Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/ Dev site: http://dev.catalyst.perl.org/
Re: [Catalyst] Mocking for Catalyst testing
Excellent!!! Just what I'd needed and never found - dunno why. I kind of improvised part of this (I used remote and local versions of a model elsewhere), but this work work just fine for me, and I can use it to simplify the other usages as well. I can then fake everything I need through configuration. All the best Stuart On 2010-01-17, at 8:56 PM, Tomas Doran wrote: On 17 Jan 2010, at 22:07, Stuart Watt wrote What I wanted to be able to do was mock out the remote job queue web service, so at least I could check out that the front end render stuff without to put an entire web server temporarily in place. Basically, I'd hoped to replace the model that implements the web service and check that the right stuff goes in and out. Right - and _THIS_ is why you implement your model classes outside of Catalyst, and bind them in. If you are using Catalyst::Model::Adaptor to bind an external class, then changing the implementation of the model class to a mock class in your test suite is a simple case of providing some config to change the name of the class which is adapted... Cheers t0m ___ List: Catalyst@lists.scsys.co.uk Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/ Dev site: http://dev.catalyst.perl.org/ -- This message was scanned by ESVA and is believed to be clean. Click here to report this message as spam.http://antispam.infobal.com/cgi-bin/learn-msg.cgi?id=358052807F.E175C ___ List: Catalyst@lists.scsys.co.uk Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/ Dev site: http://dev.catalyst.perl.org/
[Catalyst] Mocking for Catalyst testing
OK, so it's not solely a Catalyst question, but I'd be keen for any pointers on good tools for mocking when testing Catalyst applications. I'm not especially needing mocking databases, i.e., I've seen DBD::Mock, but I want to test using mocks of some other model components. The one that causes most grief is a web service client component. I have tests for this independently, and wanted to be able to test the Catalyst application using a mock for this, but having tried Test::MockClass and Test::Mock::Class neither seemed easy to get to work as a mock. Does anyone have a good recipe for mocking Catalyst components? Any suggestions for good tools/modules to use? All the best Stuart ___ List: Catalyst@lists.scsys.co.uk Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/ Dev site: http://dev.catalyst.perl.org/