Giter Club home page Giter Club logo

mongoid-rspec's Introduction

Gem Version Test Status Rubocop Status

The mongoid-rspec library provides a collection of RSpec-compatible matchers that help to test Mongoid documents.

Tested against:

  • MRI: 2.6.x, 2.7.x, 3.0.x, 3.1.x, 3.2.x
  • Mongoid: 4, 5, 6, 7, 8, 9

See .github/workflows/rspec.yml for the latest test matrix.

Installation

Drop this line into your Gemfile:

group :test do
  gem 'mongoid-rspec'
end

Compatibility

This gem is compatible with Mongoid 3, 4, 5, 6, 7, 8, 9.

Configuration

Rails

Add to your rails_helper.rb file

require 'mongoid-rspec'

RSpec.configure do |config|
  config.include Mongoid::Matchers, type: :model
end

Other

Add to your spec_helper.rb file.

require 'mongoid-rspec'

RSpec.configure do |config|
  config.include Mongoid::Matchers
end

Matchers

be_mongoid_document

class Post
  include Mongoid::Document
end

RSpec.describe Post, type: :model do
  it { is_expected.to be_mongoid_document }
end

be_dynamic_document

class User
  include Mongoid::Document
  include Mongoid::Attributes::Dynamic
end

RSpec.describe User, type: :model do
  it { is_expected.to be_dynamic_document }
end

have_timestamps

With full timestamps.

class Log
  include Mongoid::Document
  include Mongoid::Timestamps
end

RSpec.describe Log, type: :model do
  it { is_expected.to have_timestamps }
end

With short timestamps.

class User
  include Mongoid::Document
  include Mongoid::Timestamps::Short
end

RSpec.describe User, type: :model do
  it { is_expected.to have_timestamps.shortened }
end

With only creating or updating timestamps.

class Admin
  include Mongoid::Document
  include Mongoid::Timestamps::Create
  include Mongoid::Timestamps::Update
end

RSpec.describe Admin, type: :model do
  it { is_expected.to have_timestamps.for(:creating) }
  it { is_expected.to have_timestamps.for(:updating) }
end

With short creating or updating timestamps.

class Post
  include Mongoid::Document
  include Mongoid::Timestamps::Create::Short
end

RSpec.describe Short, type: :model do
  it { is_expected.to have_timestamps.for(:creating).shortened }
end

be_stored_in

class Post
  include Mongoid::Document

  store_in database: 'db1', collection: 'messages', client: 'secondary'
end

RSpec.describe Post, type: :model do
  it { is_expected.to be_stored_in(database: 'db1', collection: 'messages', client: 'secondary') }
end

It checks only those options, that you specify. For instance, test in example below will pass, even though expectation contains only database option.

class Comment
  include Mongoid::Document

  store_in database: 'db2', collection: 'messages'
end

RSpec.describe Comment, type: :model do
  it { is_expected.to be_stored_in(database: 'db2') }
end

It works fine with lambdas and procs.

class User
  include Mongoid::Document

  store_in database: ->{ Thread.current[:database] }
end

RSpec.describe Post, type: :model do
  it do
    Thread.current[:database] = 'db3'
    is_expected.to be_stored_in(database: 'db3')

    Thread.current[:database] = 'db4'
    is_expected.to be_stored_in(database: 'db4')
  end
end

have_index_for

class Article
  index({ title: 1 }, { unique: true, background: true })
  index({ title: 1, created_at: -1 })
  index({ category: 1 })
end

RSpec.describe Article, type: :model do
  it do
    is_expected
      .to have_index_for(title: 1)
      .with_options(unique: true, background: true)
  end
  it { is_expected.to have_index_for(title: 1, created_at: -1) }
  it { is_expected.to have_index_for(category: 1) }
end

Field Matchers

RSpec.describe Article do
  it { is_expected.to have_field(:published).of_type(Mongoid::Boolean).with_default_value_of(false) }
  it { is_expected.to have_field(:allow_comments).of_type(Mongoid::Boolean).with_default_value_of(true) }
  it { is_expected.not_to have_field(:allow_comments).of_type(Mongoid::Boolean).with_default_value_of(false) }
  it { is_expected.not_to have_field(:number_of_comments).of_type(Integer).with_default_value_of(1) }
end

RSpec.describe User do
  it { is_expected.to have_fields(:email, :login) }
  it { is_expected.to have_field(:s).with_alias(:status) }
  it { is_expected.to have_fields(:birthdate, :registered_at).of_type(DateTime) }
end

Association Matchers

RSpec.describe User do
  it { is_expected.to have_many(:articles).with_foreign_key(:author_id).ordered_by(:title) }

  it { is_expected.to have_one(:record) }

  # can verify autobuild is set to true
  it { is_expected.to have_one(:record).with_autobuild }

  it { is_expected.to have_many :comments }

  # can also specify with_dependent to test if :dependent => :destroy/:destroy_all/:delete is set
  it { is_expected.to have_many(:comments).with_dependent(:destroy) }

  # can verify autosave is set to true
  it { is_expected.to have_many(:comments).with_autosave }

  it { is_expected.to embed_one :profile }

  it { is_expected.to have_and_belong_to_many(:children) }
  it { is_expected.to have_and_belong_to_many(:children).of_type(User) }
end

RSpec.describe Profile do
  it { is_expected.to be_embedded_in(:user).as_inverse_of(:profile) }
end

RSpec.describe Article do
  it { is_expected.to belong_to(:author).of_type(User).as_inverse_of(:articles) }
  it { is_expected.to belong_to(:author).of_type(User).as_inverse_of(:articles).with_index }
  it { is_expected.to embed_many(:comments) }
end

# when the touch option is provided, then we can also verify that it is set

# by default, with_touch matches true when no parameters are provided
describe Article do
  it { is_expected.to belong_to(:author).of_type(User).as_inverse_of(:articles).with_index.with_touch }
end

# parameters are supported for explicit matching
describe Comment do
  it { is_expected.to be_embedded_in(:article).as_inverse_of(:comments).with_polymorphism.with_touch(true) }
end

describe Permalink do
  it { is_expected.to be_embedded_in(:linkable).as_inverse_of(:link).with_touch(false) } 
end

# touch can also take a symbol representing a field on the parent to touch
describe Record do
  it { is_expected.to belong_to(:user).as_inverse_of(:record).with_touch(:record_updated_at) }
end

RSpec.describe Comment do
  it { is_expected.to be_embedded_in(:article).as_inverse_of(:comments) }
  it { is_expected.to belong_to(:user).as_inverse_of(:comments) }
end

RSpec.describe Record do
  it { is_expected.to belong_to(:user).as_inverse_of(:record) }
end

RSpec.describe Site do
  it { is_expected.to have_many(:users).as_inverse_of(:site).ordered_by(:email.asc).with_counter_cache }
end

RSpec.describe Message do
  it { is_expected.to belong_to(:user).with_optional }
end

Validation Matchers

RSpec.describe Site do
  it { is_expected.to validate_presence_of(:name) }
  it { is_expected.to validate_uniqueness_of(:name) }
end

RSpec.describe User do
  it { is_expected.to validate_presence_of(:login) }
  it { is_expected.to validate_uniqueness_of(:login).scoped_to(:site) }
  it { is_expected.to validate_uniqueness_of(:email).case_insensitive.with_message("is already taken") }
  it { is_expected.to validate_format_of(:login).to_allow("valid_login").not_to_allow("invalid login") }
  it { is_expected.to validate_associated(:profile) }
  it { is_expected.to validate_exclusion_of(:login).to_not_allow("super", "index", "edit") }
  it { is_expected.to validate_inclusion_of(:role).to_allow("admin", "member") }
  it { is_expected.to validate_confirmation_of(:email) }
  it { is_expected.to validate_presence_of(:age).on(:create, :update) }
  it { is_expected.to validate_numericality_of(:age).on(:create, :update) }
  it { is_expected.to validate_inclusion_of(:age).to_allow(23..42).on([:create, :update]) }
  it { is_expected.to validate_presence_of(:password).on(:create) }
  it { is_expected.to validate_presence_of(:provider_uid).on(:create) }
  it { is_expected.to validate_inclusion_of(:locale).to_allow([:en, :ru]) }
end

RSpec.describe Article do
  it { is_expected.to validate_length_of(:title).within(8..16) }
  it { is_expected.to validate_absence_of(:deletion_date) }
end

RSpec.describe Visitor do
  it { is_expected.to validate_length_of(:name).with_maximum(160).with_minimum(1) }
end

RSpec.describe Profile do
  it { is_expected.to validate_numericality_of(:age).greater_than(0) }
end

RSpec.describe MovieArticle do
  it { is_expected.to validate_numericality_of(:rating).to_allow(:greater_than => 0).less_than_or_equal_to(5) }
  it { is_expected.to validate_numericality_of(:classification).to_allow(:even => true, :only_integer => true, :nil => false) }
end

RSpec.describe Person do
   # in order to be able to use the custom_validate matcher, the custom validator class (in this case SsnValidator)
   # should redefine the kind method to return :custom, i.e. "def self.kind() :custom end"
  it { is_expected.to custom_validate(:ssn).with_validator(SsnValidator) }
end

# If you're using validators with if/unless conditionals, spec subject must be object instance
# This is supported on Mongoid 4 and newer
Rspec.describe User do
  context 'when user has `admin` role' do
    subject { User.new(role: 'admin') }

    it { is_expected.to validate_length_of(:password).greater_than(20) }
  end

  context 'when user does not have `admin` role' do
    subject { User.new(role: 'member') }

    it { is_expected.not_to validate_length_of(:password) }
  end
end

Mass Assignment Matcher

RSpec.describe User do
  it { is_expected.to allow_mass_assignment_of(:login) }
  it { is_expected.to allow_mass_assignment_of(:email) }
  it { is_expected.to allow_mass_assignment_of(:age) }
  it { is_expected.to allow_mass_assignment_of(:password) }
  it { is_expected.to allow_mass_assignment_of(:password) }
  it { is_expected.to allow_mass_assignment_of(:role).as(:admin) }

  it { is_expected.not_to allow_mass_assignment_of(:role) }
end

Accepts Nested Attributes Matcher

RSpec.describe User do
  it { is_expected.to accept_nested_attributes_for(:articles) }
  it { is_expected.to accept_nested_attributes_for(:comments) }
end

RSpec.describe Article do
  it { is_expected.to accept_nested_attributes_for(:permalink) }
end

Contributing

You're encouraged to contribute to this library. See CONTRIBUTING for details.

Copyright and License

Copyright (c) 2009-2018 Evan Sagge and Contributors.

MIT License. See LICENSE for details.

mongoid-rspec's People

Contributors

akarmes avatar c0va23 avatar ches avatar chocoken517 avatar coffeencoke avatar dblock avatar did avatar durran avatar evansagge avatar francescobbo avatar glebtv avatar javierav avatar jc00ke avatar johnnyshields avatar kfaustino avatar knovoselic avatar matheusvetor avatar mkempe avatar mstarkman avatar nanocity avatar nessche avatar nofxx avatar ph avatar pranas avatar rodrigopinto avatar rodrigues avatar sairamsrinivasan avatar shingara avatar taiki45 avatar yairgo 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  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mongoid-rspec's Issues

Spec fails when model is embedded twice in the same model

I have valid mongoid documents like this:

class ScheduleValue
  include Mongoid::Document

  embedded_in :schedule, :inverse_of => :desired
  embedded_in :schedule, :inverse_of => :actual
end
class Schedule
  include Mongoid::Document

  embeds_one :desired, :class_name => "ScheduleValue", :inverse_of => :desired
  accepts_nested_attributes_for :desired

  embeds_one :actual, :class_name => "ScheduleValue", :inverse_of => :actual
  accepts_nested_attributes_for :actual
end

but the following fails

describe ScheduleValue do
  it {should be_embedded_in(:schedule).as_inverse_of(:desired)}
  it {should be_embedded_in(:schedule).as_inverse_of(:actual)}
end
Expected ScheduleValue to be embedded in "schedule" which is an inverse of "desired", got ScheduleValue is embedded in schedule which is an inverse of actual

version mismatch

When i try to do bundle install using mongoid 2.0.0.beta.13 i get the following error:

Bundler could not find compatible versions for gem "mongoid":
In Gemfile:
mongoid-rspec (= 1.1.2) depends on
mongoid (>= 2.0.0.beta3)

mongoid (2.0.0.beta.13)

is it because of the naming change (2.0.0.betaX vs 2.0.0.beta.X)?

Field type validation not quite working.

Failure/Error: it { should have_fields(:email, :password, :name).of_type(String) }
       Expected Account to have fields named "email", "password", and "name" of type String, got field "email" of type Mongoid::Fields::Serializable::Object, field "password" of type Mongoid::Fields::Serializable::Object, and field "name" of type Mongoid::Fields::Serializable::Object

uninitialized constant error in polymorphic reference

Using Mongoid 2.0.0.beta.20 and having an embedded document Comment:

class Comment 
  include Mongoid::Document
  embedded_in :commentable, :inverse_of => :comments
end

and this spec:

describe Comment do
   describe "associations" do
      it { should be_embedded_in(:commentable).as_inverse_of(:comments) }
   end
end

I get this output:

Failures:
1) Comment associations 
Failure/Error: it { should be_embedded_in(:commentable).as_inverse_of(:comments) }
 NameError:
   uninitialized constant Commentable

include Mongoid::Timestamps::Created

My model created a new field :created_at when a new object is created.

With:

it { should be_timestamped_document } 

I can not pass the test.

How can I pass the test for

include Mongoid::Timestamps::Created

Thank you!

Mongoid-rspec with Factory girl

Hey, I am using mongoid-rspec.
As per the README, my validation and association specs such as
describe User do
it { should validate_presence_of(:email) }
it { should validate_presence_of(:password) }
it { should have_many(:accounts).with_dependent(:delete) }
it { should embed_one :address }
end
runs very well.

I have created factory for user at spec/factories/user.rb as,
Factory.define :user do |u|
u.email "test@test_user.com"
u.password "123456"
end

When I add a line to specs such as
it { should save }
it gives me error as ,
Failures:

  1. User
    Failure/Error: it { should save }
    NameError:
    undefined local variable or method `save' for #<RSpec::Core::ExampleGroup::Nested_1:0xb31ca58 @example=nil>

    ./spec/models/user_spec.rb:5:in`block (2 levels) in <top (required)>'

Can anyone tell me why it is not working?

undefined error 'has_fields?' with a simple spec

I wanted to try Mongoid in a Rails 3 app, so as part of my preparations I also installed mongoid-rspec. I added it to the Gemfile and added "require 'mongoid-rspec'" to spec/spec_helper.

I have this simple document:

class Post
  include Mongoid::Document
  field :title
  field :body
end

and a spec:

require 'spec_helper'
describe Post do
  it { should have_fields(:title, :body) }
end

Then rake spec exits with the following error:

/usr/bin/ruby1.8 -S bundle exec rspec "./spec/models/post_spec.rb"
F

Failures:
  1) Post 
     Failure/Error: it { should have_fields(:title, :body) }
     undefined method `has_fields?' for #<Post _id: 4c8e6d26e3c7390c4a000001, body: nil, title: nil>
     # ./spec/models/post_spec.rb:5

I guess it's something trivial that I didn't do or configure?

validate_length_of conditions

I'm not able to specify conditions on validate_length_of

Failure/Error: it { should validate_length_of(:password).greater_than(5) }
 NoMethodError:
   undefined method `greater_than' for #<Mongoid::Matchers::Validations::ValidateLengthOfMatcher:0xb726f04>


 Failure/Error: it { should validate_length_of(:password).to_allow(greater_than: 5) }
 NoMethodError:
   undefined method `to_allow' for #      <Mongoid::Matchers::Validations::ValidateLengthOfMatcher:0x9cb3834>


Failure/Error: it { should validate_length_of(:password).within(5..15) }
 ArgumentError:
   bad value for range

I'm on rails 3.2.9, mongoid 3.0.14, rspec 2.12.0, & mongoid-rspec 1.5.5

Can't get it to work

Hi,

I'm working on a Rails 3.1 application with mongoid (2.0.2) and rspec (2.6.0).

I added gem 'mongoid-rspec' to my Gemfile and added the configuration file in spec/support.

My model is the following :

class Folder
  include Mongoid::Document
  ...
  has_many :documents
end

and my test is the following :

describe Folder do
  it { should have_many(:documents) }
end

I checked that the spec_helper.rb requires the files in support.
When I launch my test, it tells me this:

undefined method `has_many?' for #<Folder:0x00000101461250>

Did I miss something ?

Thanks

have_index_for :id

it { should have_index_for(:id) }

Rspec Matcher doesn't work for id's.

HaveFieldMatcher gives a false positive when using with_default_value_of of false or nil

class Blog
  include Mongoid::Document
  field :popular, :type => Boolean, :default => true
end

Blog.should have_field(:popular).of_type(Boolean).with_default_value_of(false) # passes

The reason is this line checks the value of @default instead of checking if the @default instance variable is defined.

if @default and @klass.fields[attr].default != @default
  error << " with default value of #{@klass.fields[attr].default}"
end

Errors make no sense?

Expected User to validate presence of "provider_uid"; instead got "presence" validator on "provider_uid" on methods: 
     # ./spec/models/user_spec.rb:4:in `block (2 levels) in <top (required)>'

The idea was to see if it actually works, by removing on: :create from my validator and then leaving on([:create]) on the spec test, and even though it works the error makes absolutely no sense IMO because to me it's saying "didn't find the presence validator, I just found a presence validator for it" :P

Updating to latest Mongoid

Using bundler with the Gem, I get this error:
Bundler could not find compatible versions for gem "mongoid":
In Gemfile:
mongoid-rspec depends on
mongoid (= 2.0.0.beta4)

    mongoid (2.0.0.beta10)

Using Bundler with the latest git repository I get:
Could not find gem 'mongoid (>= 2.0.0.beta3)', required by 'mongoid-rspec', in any of the sources

Support for Mongoid 2.0.0.rc.1

mongoid-rspec is broken on Mongoid 2.0.0.rc.1, because mongoid/associations file is no longer present?

Would you be able to take a look and, if at all possible, please provide a fix? I'm afraid I don't have the time to contribute one myself.

how to test :class =>

How can I test this
embeds_many :input_datas, :class => "Result"

I did
it { should embed_many(:input_datas) }

but how do I test class??
please help! _

HaveAssociationMatcher raises for polymorphic embedded_in

We have a polymorphic Address model/document that we define as

embedded_in :addressable, :inverse_of => :address

If we write the expected matcher

it { should be_embedded_in(:addressable).as_inverse_of(:address) }

Then we get an exception because the matcher tries to call association.klass, and Mongoid dutifully tries to constantize Addressable. The HaveAssociationMatcher shouldn't be calling association.klass unless I specify a as_type_of requirement.

bson_ext requirement breaks jruby

I noticed your gemspec requires bson_ext, which breaks jruby. Since bson_ext is optional with both the ruby mongo driver and mongoid, is this explicit requirement needed in mongoid-rspec?

Add the new document matchers to the README

These are available but not in the README.

it { should be_embedded_document }
it { should be_timestamped_document }
it { should be_versioned_document }
it { should be_paranoid_document }

Would be good to keep the README up to date with every single test available.

field localized check

It would be great to have a method for checking if the field is localized:

it { should have_fields(:name).localized

LocalJumpError when testing for unique indexes

When testing for a unique index on a model, way the title on an article I get this error:

LocalJumpError:
  no block given (yield)

This is coming from this line in my article_spec.rb file:

it { should have_index_for(:title).with_options(unique: true) }

I looked around google, but there is very little. Some fixed it by installing should (I guess for sqlite3, not(!) mongoid).

ObjectId not supported

it { should have_fields(:product_id).of_type(Moped::BSON::ObjectId) }

ObjectId is not recognised.

breaks with rspec-rails 2.6.0

Getting these errors after upgrading:

Failure/Error: it { should be_mongoid_document }
NoMethodError:
undefined method `matches?' for #Class:#RSpec::Matchers::Matcher:0x000001084fb1f0

 Failure/Error: it { should be_timestamped_document }
 NoMethodError:
   undefined method `field' for #<Class:#<RSpec::Matchers::Matcher:0x00000104e96ab0>>

requires mongoid (~> 2.0.0.rc.7)

Gemfile should reference the latest mongoid, as the requirement for rc.7 also comes to the Gemfile.lock of my project and thus gets loaded...

SaveMatcher overwrites default #save behavior

The SaveMatcher currently overwrites the original implementation of my_document#save.

This was very frustrating when testing an after_create callback, that was only called when using #save! instead.

I think only :save_properly should return a matcher, #save should be untouched.

symbolize keys for Index Matcher

Mongoid symbolizes the index keys, so maybe the matcher should accept string and symbol keys? example:

class ExampleClass
include Mongoid::Document
embeds_many :example_embedded_class
index 'example_embedded_classes.name' => 1
end

class ExampleEmbeddedClass
include Mongoid::Document
field :name
embedded_in :example_class
end

describe ExampleClass do
it { should have_index_for 'example_embedded_classes.name' => 1 }
it { should have_index_for :'example_embedded_classes.name' => 1 }
end

Failures:

  1. ExampleClass
    Failure/Error: it { should have_index_for 'example_embedded_classes.name' => 1 }
    Expected ExampleClass to have an index for {"example_embedded_classes.name"=>1}, got no index for {"example_embedded_classes.name"=>1}

Inclusion validator does not support ranges

I have the following validator validates :maximum_amount, numericality: true, inclusion: { in: 1..16 } in my model and in the spec i have it { should validate_inclusion_of(:maximum_amount).to_allow(1..16) } which fails with can't convert Range into Array because you have not_allowed_values = @allowed_values - @validator.options[:in].

Bundler conflict

When adding mongoid-rspec to the Gemfile and run $ bundle install, bundler returns the following error:

Bundler could not find compatible versions for gem "mongoid":
In Gemfile:
mongoid-rspec depends on
mongoid (= 2.0.0.beta4)

mongoid (2.0.0.beta.16)

I don't know why this error is appearing, I guess it may be solved by updating the gem's mongoid dependencie.

Thanks,
Rodrigo Alves Vieira - http://rodrigo3n.com

validate if

Could you please tell me how to check validation like:

  validates :foo,   presence: true, if: ->() { condition? }

Thanks in advance!

Rake aborted on association mismatch

Hi,

When I run a "should reference_many" on something that is an inverse, the spec runner proc is aborted and there is a back trace left. Expectation would be that the spec runner proc would not be aborted.


Model:
references_many :users, :stored_as => :array, :inverse_of => :accounts

Spec:
it { should reference_many :users }

Result:

  1. Account an account should not be created without a user_id
    Failure/Error: it { should reference_many :users }
    Unknown association type

    /Users/danielcroft/.rvm/gems/ruby-1.8.7-p299@SimplePRWebsite/gems/mongoid-rspec-1.2.0/lib/matchers/associations.rb:106:in `type_description'

    /Users/danielcroft/.rvm/gems/ruby-1.8.7-p299@SimplePRWebsite/gems/mongoid-rspec-1.2.0/lib/matchers/associations.rb:53:in`matches?'

    ./spec/models/account_spec.rb:80

    /Users/danielcroft/.rvm/gems/ruby-1.8.7-p299@SimplePRWebsite/gems/activesupport-3.0.0.rc/lib/active_support/dependencies.rb:219:in `inject'

    ./spec/controllers/accounts_controller_spec.rb:3

rake aborted!
... (back trace continues here)

Expected result:

  1. Account an account should not be created without a user_id
    Failure/Error: it { should reference_many :users }
    Unknown association type

    /Users/danielcroft/.rvm/gems/ruby-1.8.7-p299@SimplePRWebsite/gems/mongoid-rspec-1.2.0/lib/matchers/associations.rb:106:in `type_description'

    /Users/danielcroft/.rvm/gems/ruby-1.8.7-p299@SimplePRWebsite/gems/mongoid-rspec-1.2.0/lib/matchers/associations.rb:53:in`matches?'

    ./spec/models/account_spec.rb:80

    /Users/danielcroft/.rvm/gems/ruby-1.8.7-p299@SimplePRWebsite/gems/activesupport-3.0.0.rc/lib/active_support/dependencies.rb:219:in `inject'

    ./spec/controllers/accounts_controller_spec.rb:3


Regards,
Daniel

undefined method `has_fields?'

Hello

I just installed mongoid-spec 1.2.1 alongside mongoid 2.0.0.beta.17 and Rails 3.0.0. I have the following model spec:

require 'spec_helper'
describe Article do
  it { should have_field(:headword) }
end

article.rb:

class Article
  include Mongoid::Document
  field :headword
end

When I run the spec, I get the following error:

Failures:
  1) Article 
     Failure/Error: it { should have_field(:headword) }
     undefined method `has_field?' for #<Article:0x102bc6008>
     # /Users/martin/.rvm/gems/ruby-1.8.7-p302/gems/mongoid-2.0.0.beta.17/lib/mongoid/attributes.rb:23:in `method_missing'
     # ./spec/models/article_spec.rb:5
     # /Users/martin/.rvm/gems/ruby-1.8.7-p302/gems/activesupport-3.0.0/lib/active_support/dependencies.rb:239:in `inject'

Any idea what could be causing this?

Best regards,
Martin Solli

Embedded key indexes not being validated properly.

index({ 'provider.uid' => 1 }, { unique: true, name: 'provider_uid_index', background: false })

I have an index like the above, however when I try to run a spec against it using should have_index_for('provider.uid') the test fails, is there something I'm missing that is not in the documentation? The same syntax fails for associated indexes... for example when you use has_many and you put an index on pages.slug (bad example I know but it demonstrates what I mean)

BigDecimal with default value fails

I have a field with BigDecimal type and default value of 0.

field :balance, :type => BigDecimal, :default => BigDecimal.new("0")

But following spec fails

it { should have_field(:balance).of_type(BigDecimal).with_default_value_of(BigDecimal.new("0")) }

with error msg

Expected Balance to have fields named "balance"  of type BigDecimal with default value of #<BigDecimal:109aa7148,'0.0',9(18)>, got field "balance" with default value of 0.0

While debugging, the default value seems to be right. Here is object(@klass)

#<Mongoid::Field:0x00000105813fc0 @type=BigDecimal, @label=nil, @default=#<BigDecimal:105814268,'0.0',9(18)>, @name="frozen_balance", @copyable=false, @options={:type=>BigDecimal, :default=>#<BigDecimal:105814268,'0.0',9(18)>}>

but @klass.default gives "0.0" and this is causing test to fail

HaveAssociationMatcher raises on polymorphic associations

The HaveAssociationMatcher doesn't consider the possibility that the association may not specify an actual class -- for example, when the embedded_in target is polymorphic. Here's a minimal example:

# in model
class Permalink
  embedded_in :linkable, :inverse_of => :link
end

# in spec
describe Permalink do
  it { should be_embedded_in :linkable }
end

This will raise on line 75 of lib/matchers/association.rb, when the result message tries to look up metadata.klass and thus resolve the class. In turn, this fails because there is no class to resolve :linkable to.

mongoid 3.0 compatibility

Hey,

This isn't a bug fix request, per se. Rather, I'm wondering whether the dependency specification in the gemspec could be changed to allow use with mongoid 3 (3.0.0.rc)? I'd love to check out mongoid 3, but all my tests are in mongoid-rspec . Any comment on when/whether compatibility might occur would be much appreciated.

G

undefined method `has_index_for?' for

Hi all,

When I added test to index. I get following errors:

  1) Mobile 
     Failure/Error: it { should have_index_for(:uid) }
     NoMethodError:
       undefined method `has_index_for?' for #<Mobile:0xbce87f8>
     # ./spec/models/mobile_spec.rb:17:in `block (2 levels) in <top (required)>'

Syntax:

it { should have_index_for(:uid) }

Here is my environment information:

  • rails 3.2.1
  • mongoid 2.4.3
  • mongoid-rspec 1.4.4

Gem release

Doesn't the index matchers warrant a gem release? :)

Thanks for your work, btw.

RubyGems 1.4.2 issue with Bundler - Can't install gem

Since upgrading to RubyGems 1.4.2, I'm now getting:

Bundler could not find compatible versions for gem "mongoid":
In Gemfile:
mongoid-rspec depends on
mongoid (~> 2.0.0)

mongoid (2.0.0.rc.5)

I have rc.5 installed

Support rspec 2.10.0

We cannot upgrade to rspec 2.10.0 because of mongoid-rspec.

Why not supporting rspec >= 2.9?

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.