Skip to content

Instantly share code, notes, and snippets.

@TomK32
Created April 12, 2010 21:17
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save TomK32/364006 to your computer and use it in GitHub Desktop.
Save TomK32/364006 to your computer and use it in GitHub Desktop.
Example for testing mongoid assocications with Rspec/shoulda
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe Album do
before :each do
@website = Factory(:website)
@album = @website.albums.build(Factory(:album).attributes)
@album.save
end
it "should be valid" do
@album.should be_valid
end
describe "validations" do
it { should validate_presence_of(:title) }
it { should validate_presence_of(:body) }
it { should validate_presence_of(:body_html) }
end
describe "associations" do
it "should be embedded in a website" do
association = Page.associations['website']
association.klass.should ==(Website)
association.association.should ==(Mongoid::Associations::EmbeddedIn)
association.inverse_of.should ==(:pages)
end
end
end
# This file is copied to ~/spec when you run 'ruby script/generate rspec'
# from the project root directory.
ENV["RAILS_ENV"] ||= 'test'
require 'factory_girl'
require File.dirname(__FILE__) + "/../config/environment" unless defined?(Rails.root)
require 'rails/test_help'
require 'rspec/rails'
require 'shoulda'
require 'shoulda/active_model'
require 'mongoid'
require 'webrat'
require 'rspec/expectations'
# Requires supporting files with custom matchers and macros, etc,
# in ./support/ and its subdirectories.
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
Rspec.configure do |config|
config.include Rspec::Matchers
# requires this branch:
# http://github.com/TomK32/shoulda/commits/master
config.include Shoulda::ActiveModel::Matchers
config.mock_with :rspec
end
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe Website do
before(:each) do
@website = Factory(:website)
end
it "@website should be valid" do
@website.should be_valid
end
describe "associations" do
# I'm just waiting for an has_many_related :in => :user_ids like mongo_mapper has
it "should have user_ids" do
@website.user_ids.should be_a(Array)
@website.user_ids.first.should be_a(String)
User.find(@website.user_ids[0]).id.should ==(@website.user_ids[0])
end
it "should embed a theme" do
association = Website.associations['theme']
association.klass.should ==(Theme)
association.association.should ==(Mongoid::Associations::EmbedsOne)
end
it "should embed many albums" do
association = Website.associations['albums']
association.klass.should ==(Album)
association.association.should ==(Mongoid::Associations::EmbedsMany)
end
end
end
@rafaels
Copy link

rafaels commented Jul 18, 2012

Did you continued with this!?

@TomK32
Copy link
Author

TomK32 commented Jul 18, 2012

Oh gosh, just use the mongoid rspec gem, that does all the tricks you need.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment