Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial support of associations.

  • Loading branch information...
commit a0c95202be56083c0251b188ba4d19162d1691da 1 parent 0a967b6
@nmerouze nmerouze authored
View
66 lib/remarkable_mongomapper/matchers/association_matcher.rb
@@ -0,0 +1,66 @@
+module Remarkable
+ module MongoMapper
+ module Matchers
+ class AssociationMatcher < Remarkable::MongoMapper::Base
+ arguments :type, :collection => :associations, :as => :association
+
+ collection_assertions :association_exists?, :type_matches?
+
+ protected
+
+ def association_exists?
+ reflection
+ end
+
+ def type_matches?
+ reflection.type == @type
+ end
+
+ def reflection
+ @reflection ||= subject_class.associations[@association]
+ end
+
+ def interpolation_options
+ options = {}
+ options[:type] = Remarkable.t(@type, :scope => matcher_i18n_scope, :default => @type.to_s.gsub("_", ""))
+
+ if @subject && reflection
+ options.merge!(
+ :actual_type => Remarkable.t(reflection.type, :scope => matcher_i18n_scope, :default => reflection.type.to_s)
+ )
+ end
+
+ options
+ end
+
+ end
+
+ # Ensures that the many relationship exists. Will also test that the
+ # associated table has the required columns.
+ #
+ # == Examples
+ #
+ # should_have_many :addresses
+ #
+ # it { should have_many(:addresses) }
+ #
+ def have_many(*associations, &block)
+ AssociationMatcher.new(:many, *associations, &block).spec(self)
+ end
+
+ # Ensures that the many relationship exists. Will also test that the
+ # associated table has the required columns.
+ #
+ # == Examples
+ #
+ # should_belong_to :user
+ #
+ # it { should belong_to(:user) }
+ #
+ def belong_to(*associations, &block)
+ AssociationMatcher.new(:belongs_to, *associations, &block).spec(self)
+ end
+
+ end
+ end
+end
View
2  lib/remarkable_mongomapper/matchers/have_key_matcher.rb
@@ -1,7 +1,7 @@
module Remarkable
module MongoMapper
module Matchers
- class HaveKeyMatcher < Remarkable::MongoMapper::Base #:nodoc:
+ class HaveKeyMatcher < Remarkable::MongoMapper::Base
arguments :collection => :attributes, :as => :attribute
View
4 lib/remarkable_mongomapper/matchers/validate_presence_of_matcher.rb
@@ -1,7 +1,7 @@
module Remarkable
module MongoMapper
module Matchers
- class ValidatePresenceOfMatcher < Remarkable::MongoMapper::Base #:nodoc:
+ class ValidatePresenceOfMatcher < Remarkable::MongoMapper::Base
arguments :collection => :attributes, :as => :attribute
optional :message
@@ -13,8 +13,6 @@ class ValidatePresenceOfMatcher < Remarkable::MongoMapper::Base #:nodoc:
def allow_nil?
bad?(nil, :message)
- #@subject.send("#{@key}=", nil)
- #!@subject.valid? && !@subject.errors.on(@key).blank?
end
end
View
8 locales/en.yml
@@ -1,6 +1,14 @@
en:
remarkable:
mongo_mapper:
+ association:
+ many: have many
+ belongs_to: belong to
+ description: "{{type}} {{associations}}"
+ expectations:
+ association_exists: "{{subject_name}} records {{type}} {{association}}, but the association does not exist"
+ type_matches: "{{subject_name}} records {{type}} {{association}}, got {{subject_name}} records {{actual_type}} {{association}}"
+
have_key:
description: "have key(s) {{attributes}}"
expectations:
View
74 spec/matchers/association_matcher_spec.rb
@@ -0,0 +1,74 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+describe 'association_matcher' do
+ subject do
+ Article.new
+ end
+
+ describe 'have_many' do
+
+ describe 'messages' do
+ it 'should contain a description' do
+ matcher = have_many(:comments)
+ matcher.description.should == 'have many comments'
+ end
+
+ it 'should set association_exists? message' do
+ matcher = have_many(:whatever)
+ matcher.matches?(subject)
+ matcher.failure_message.should == 'Expected Article records have many whatever, but the association does not exist'
+ end
+
+ it 'should set type_matches? message' do
+ matcher = belong_to(:comments)
+ matcher.matches?(subject)
+ matcher.failure_message.should == 'Expected Article records belong to comments, got Article records have many comments'
+ end
+ end
+
+ describe 'matchers' do
+ it { should have_many(:comments) }
+ it { should_not belong_to(:comment) }
+ end
+
+ describe 'macros' do
+ should_have_many :comments
+ should_not_belong_to :comment
+ end
+
+ end
+
+ describe 'belong_to' do
+
+ describe 'messages' do
+ it 'should contain a description' do
+ matcher = belong_to(:user)
+ matcher.description.should == 'belong to user'
+ end
+
+ it 'should set association_exists? message' do
+ matcher = belong_to(:whatever)
+ matcher.matches?(subject)
+ matcher.failure_message.should == 'Expected Article records belong to whatever, but the association does not exist'
+ end
+
+ it 'should set type_matches? message' do
+ matcher = have_many(:user)
+ matcher.matches?(subject)
+ matcher.failure_message.should == 'Expected Article records have many user, got Article records belong to user'
+ end
+ end
+
+ describe 'matchers' do
+ it { should belong_to(:user) }
+ it { should_not have_many(:users) }
+ end
+
+ describe 'macros' do
+ should_belong_to :user
+ should_not_have_many :users
+ end
+
+ end
+
+end
View
15 spec/models.rb
@@ -3,4 +3,19 @@ class Article
key :title, String, :required => true
key :body, String
+
+ many :comments
+ belongs_to :user
+end
+
+class Comment
+ include MongoMapper::EmbeddedDocument
+
+ key :body, String
+end
+
+class User
+ include MongoMapper::Document
+
+ key :login, String
end
Please sign in to comment.
Something went wrong with that request. Please try again.