mongoid / mongoid-rspec Goto Github PK
View Code? Open in Web Editor NEWRSpec matchers and macros for Mongoid.
License: MIT License
RSpec matchers and macros for Mongoid.
License: MIT License
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
Is there a simple way to test :dependent => :destroy in mongoid-rspec ?
referenced_in :account
references_many :users, :dependent => :destroy
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:
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.
Doesn't the index matchers warrant a gem release? :)
Thanks for your work, btw.
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.
Mongoid supports a :scope option for validates_uniqueness_of which can't be specified using the mongoid-rspec's matcher.
See https://github.com/mongoid/mongoid/blob/master/lib/mongoid/validations/uniqueness.rb
/jzn
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.
We cannot upgrade to rspec 2.10.0 because of mongoid-rspec.
Why not supporting rspec >= 2.9?
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!
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
it { should have_fields(:product_id).of_type(Moped::BSON::ObjectId) }
ObjectId is not recognised.
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! _
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?
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
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
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.
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...
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)
Is there a reason to have mongoid-rspec dependent on a previous version of itself?
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
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:
rake aborted!
... (back trace continues here)
Expected result:
Regards,
Daniel
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
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
Could you please tell me how to check validation like:
validates :foo, presence: true, if: ->() { condition? }
Thanks in advance!
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>>
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:
Can anyone tell me why it is not working?
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
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
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]
.
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)?
It would be great to have a method for checking if the field is localized:
it { should have_fields(:name).localized
it { should validate_numericality_of(:order).as_integer_only }
Here's the error:
Bundler could not find compatible versions for gem "mongoid":
In Gemfile:
mongoid-rspec (= 1.3.2) depends on
mongoid (~> 2.0.0)
mongoid (2.0.0.beta.20)
it { should have_index_for(:id) }
Rspec Matcher doesn't work for id's.
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
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
Rubygems 1.4 changed how pre-release gem versions are compared causing the 1.3.2 gemspec to find no matching version of the mongoid gem.
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
That's all !
Mongoid gem 2.0.0 is not in the rubygems.org
So, bundle gives this error msg
Could not find gem 'mongoid (~> 2.0.0)', required by 'mongoid-rspec', in any of the sources
You should fix this http://robots.thoughtbot.com/post/2508037841/twiddle-wakka problem
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.
After bundle upgrade...
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:
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
There's no validate_exclusion_of(field).not_in(*args)
matcher.
Is it possible to implement this matcher similar to how shoulda ensure_acceptance_of works?
I have done all the necessary configuration to make mongoid-rspec work. But when I am running my specs it gives cannot load such file -- mongoid/rspec. I am using mongoid-rspec 1.2.1 and mongoid 3.0 and rspec-rails 2.9. Could anyone help me.
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?
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).
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.