Skip to content

Instantly share code, notes, and snippets.

@toniesteves
Forked from IPrism/album_spec.rb
Created March 20, 2017 17:41
Show Gist options
  • Save toniesteves/7a2c8f398d24f9be2318e584d31806c4 to your computer and use it in GitHub Desktop.
Save toniesteves/7a2c8f398d24f9be2318e584d31806c4 to your computer and use it in GitHub Desktop.
Example for testing mongoid assocications with Rspec
require 'spec_helper'
describe Album do
before(:all) do
@album = Factory.build(:album)
@website = @album.website
end
it "should be valid" do
@album.should be_valid
end
it "should require a name" do
no_name_album = @level.albums.build(name: "")
no_name_album.should_not be_valid
end
it "should require name to be unique" do
album = @level.album.new(:name => Factory.stub(:album).name)
album.save
album.should_not be_valid
end
it "should embeded by a website" do
relation = Album.relations['website']
relation.klass.should ==(Website)
relation.relation.should ==(Mongoid::Relations::Embedded::In)
end
end
require "factory_girl"
Factory.define :website, :class => Website do |w|
w.name 'www.example.com'
end
Factory.define :album, :class => Album do |a|
a.name 'Example Album'
a.website(Factory.build(:website))
end
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
# == Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
config.mock_with :rspec
require 'database_cleaner'
config.before(:suite) do
DatabaseCleaner.strategy = :truncation
DatabaseCleaner.orm = "mongoid"
end
config.before(:each) do
DatabaseCleaner.clean
end
end
require 'spec_helper'
describe Website do
before :each do
@website = Factory.build :website
@website.save
end
it "should be abble to create a website " do
@website.should be_valid
end
it "should require a name" do
no_name_website = Website.new(:name => "")
no_name_website.should_not be_valid
end
it "should requre name to be unique" do
website = Website.new(:name => Factory.stub(:website).name)
website.save
website.should_not be_valid
end
it "should embed many albums" do
relation = Website.relations['albums']
relation.klass.should ==(Album)
relation.relation.should ==(Mongoid::Relations::Embedded::Many)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment