How to use mock_models with RSpec outside of Rails?

I am currently working on a Ruby project that consists of Redis DB, EventMachine and Sinatra (for a lightweight API). We use RSpec for our testing, and up to this point everything went fine.

Now, however, we need to start talking (minimally) with the MySQL database. We decided to take the ActiveRecord route (just because it is SO glorious!), And everything has been going smoothly so far. However, we are experiencing a small problem with our tests. I found some resources on the Internet, using mock_modeland mockwith the RSpec, but I keep getting:

undefined method `mock_model'
undefined method `mock'

mistakes. What gem should I use to work without setting up the entire Rails structure (it just seems to be crowded).

OR

What other stub / gem methods should I use to mock my AR models? I really like the flexibility of the syntax, for example:

@user = double(NS::UserAccount)
@car = double(NS::Car)
NS::UserAccount.stub(:find).with(@valid_user_id).return(@user)
NS::Car.stub(:find).with(@valid_car_id).return(@car)



UPDATE

My gemfile is as follows:

group :test do
  gem 'rspec',                '~> 2.8.0'
  gem 'rspec-expectations',   '~> 2.8.0'
  gem 'rspec-mocks',          '~> 2.8.0'      
  gem 'autotest-standalone'
  gem 'em-spec',              '~> 0.2.6'
  gem 'rack-test',            '~> 0.6.1'
  gem 'factory_girl',         '~> 3.1.0'
end

Also, at the top of the file spec_helper.rb, I have the following:

require 'bundler/setup'
Bundler.require(:test, :development)
+3
source share
1 answer

For Active Record models, RSpec has a method mock_modelthat automatically creates several stubs common to all Ruby on Rails models and accepts the hash.

To use it mock_modeloutside of Rails, you had to enable Active Record and the rspec-rails extension.

To make sure you can look here where mock_modeldefined.

0
source

All Articles