Mock working in console, but not in server

35 views
Skip to first unread message

Greg Akins

unread,
Sep 25, 2013, 10:20:07 AM9/25/13
to mocha-d...@googlegroups.com
Using rails 2.1.0 and Ruby 1.8.7 with Mocha 0.14.0

I stub a class in environments/development.rb to avoid having my application contact a production server from the development environment.

EmployeeID.any_instance.stubs(:Employee).returns(9999)

If i use script/console and execute EmployeeID.find_by_Username(login).Employee returns 9999

But if I run script/server the same call returns the actual value from the database.

Any idea why the stub would be used?  

floehopper

unread,
Sep 26, 2013, 3:26:15 PM9/26/13
to mocha-d...@googlegroups.com
Hi Greg,

Wow! That's a pretty old version of Rails! Also Mocha was never really designed to do what you are trying to do i.e. stub out code in development or production environments. I'd tend to use configuration options and fake implementations [1] to do what you're doing.

However, I think I have managed to achieve what you wanted to do. Take a look at this vanilla Rails app that I created [2]. In particular, look at these lines in config/environments/development.rb [3]. Note that I had to require test-unit to get this to work. This is also something that I would recommend against doing in anything other than the test environment. I added a controller action to display the stubbed value from the server [4].

I hope that is of some help.

Cheers, James.

Reply all
Reply to author
Forward
0 new messages