Giter Club home page Giter Club logo

rspec-rails-mocha's Introduction

rspec-rails Mocha extension

Functionality of mock_model and stub_model from rspec-rails for those using Mocha.

Installation for Rails 3 (rspec-rails v2.6 and above):

# Gemfile
group :development, :test do
  gem 'rspec-rails', '~> 2.4'
end

group :test do
  gem 'rspec-rails-mocha', '~> 0.3.1', :require => false
end

# spec/spec_helper.rb
require 'rspec/rails'
require 'rspec/rails/mocha'

For Rails 2 (rspec-rails v1.3.x), use the '0.2.x' branch:

gem 'rspec-rails-mocha', '~> 0.2.1'

Then, for any version of Rails, ensure that your "spec_helper.rb" file doesn't contain config.mock_with :rspec.

Usage:

describe "Mocha plugin" do
  it "#mock_model should use given ID" do
    person = mock_model(Person, :id => 66)
    
    person.should_not be_new_record
    person.id.should == 66
  end
end

You should be able to use mock_model and stub_model in the same way you use them when mocking with rspec instead of mocha. Here are the docs.

rspec-rails-mocha's People

Contributors

jfelchner avatar mislav avatar nlalonde avatar solnic avatar tessro avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

rspec-rails-mocha's Issues

0.2.1 doesn't support Rails 2??

I have a standalone version of your rspec_rails_mocha.rb file that I use in my Rails 2.x / rspec-rails 1.3.3 code.
I thought I could use 0.2.1...but right there in the gemspec you have:
gem.add_dependency 'rspec-rails', '>= 2.0.0

and rspec-rails >= 2.0 is only for Rails3 apps.

Confused..

not working on Rails 3.0.2 and RSpec Rails 2.1.0

I got some problems... first it's raising a warning:

*****************************************************************
DEPRECATION WARNING: you are using a deprecated constant that will
be removed from a future version of RSpec.

/Users/wilkersilva/.rvm/gems/ruby-1.9.2-p0/gems/bundler-1.0.3/lib/bundler/runtime.rb:64:in `require' 

* Rspec is deprecated.
* RSpec is the new top-level module in RSpec-2
***************************************************************

and them, it still not working, Im receiving this message:

3) products/edit.html.erb renders the edit product form
   Failure/Error: @product = assign(:product, stub_model(Product,
   undefined method `stub' for #<Product _id: 1001, name: "MyString", description: "MyText">
   # ./spec/views/products/edit.html.erb_spec.rb:5:in `block (2 levels) in <top (required)>'

its like its using old methods of RSpec mock

Problems with blank? , to_a and collect

I trying to use rspec 1.2.4 with Ruby on Rails 2.3.2 but when I try this:

def make_bank_account(attributes = {})
  account = mock_model(Account)
end

These methods failed: blank? , is_a? , collect

cpk compatibility issue

Hi, using this gem with rails3 project that also uses composite primary key gem. I hit a compatibility issue between this and cpk on a to_sym call when the model has a cpk. Your thoughts?

I have a patch, I can submit a pull request if you are interested.

see below in mocha.rb line 193:

     defined?(model_class.composite?) && model_class.composite? ? primary_key = model_class.primary_key.join.to_sym : primary_key = model_class.primary_key.to_sym

Mocha deprecation warnings

Get the following warnings when I launch rspec with the latest Rails. Any chance you can fix?

*** Mocha deprecation warning: Change `require 'mocha'` to `require 'mocha/setup'`.
*** Mocha deprecation warning: Test::Unit or MiniTest must be loaded *before* Mocha.
*** Mocha deprecation warning: If you're integrating with a test library other than Test::Unit or MiniTest, you should use `require 'mocha/api'` instead of `require 'mocha'`.

How do I use it!?

Gemfile

group :test, :development do
  # gem "autotest"
  gem "capybara"   
  gem "rspec", '2.0.1'
  gem "rspec-rails", '2.0.1'
  gem 'rspec-rails-mocha'  
end

Spec

describe "User permissions to operate on Articles" do  
  before do 
    puts "User is defined" if defined? User
    puts "Article is defined" if defined? Article

    editor_user = mock_model(User)
    editor_user.stub!(:name).and_return("Editor user")
    editor_user.stub!(:role).and_return("editor")
...

Outcome

Failures:
  1) User permissions to operate on Articles should be able to :read Article he owns
     Failure/Error: editor_user = mock_model(User)
     unexpected invocation: #<Mock:User_1001>.__mock_proxy()
     unsatisfied expectations:
     - expected exactly once, not yet invoked: #<Mock:User_1001>.id(any_parameters)
     - expected exactly once, not yet invoked: #<Mock:User_1001>.persisted?(any_parameters)
     - expected exactly once, not yet invoked: #<Mock:User_1001>.destroyed?(any_parameters)
     - expected exactly once, not yet invoked: #<Mock:User_1001>.marked_for_destruction?(any_parameters)

Should I perhaps just remove the 'rspec-rails' gem?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.