Created
April 12, 2010 21:17
-
-
Save TomK32/364006 to your computer and use it in GitHub Desktop.
Example for testing mongoid assocications with Rspec/shoulda
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
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
Did you continued with this!?