Last active
August 29, 2015 14:15
-
-
Save clupprich/9b2ae833d7f9f0a95e2d to your computer and use it in GitHub Desktop.
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 'rubygems' | |
require 'bundler/setup' | |
require 'mongo_mapper' | |
logger = Logger.new(STDOUT) | |
MongoMapper.connection = Mongo::Connection.new('localhost', logger: logger) | |
MongoMapper.database = 'customers' | |
class Customer | |
include MongoMapper::Document | |
key :email, String | |
key :course, String | |
scope :no_course, where(course: '') | |
def self.with_email(email) | |
first(email: email.downcase) | |
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
require 'minitest/autorun' | |
require './customer' | |
class TestCustomer < Minitest::Test | |
def setup | |
Customer.destroy_all | |
Customer.create(email: 'customer1@test.com', course: 'algebra') | |
Customer.create(email: 'customer2@test.com', course: '') | |
end | |
def test_course_scope | |
assert_equal 1, Customer.no_course.count | |
end | |
def test_finder | |
refute_nil Customer.with_email('CUSTOMER1@TEST.COM') | |
end | |
def test_course_scope_and_finder | |
assert_nil Customer.no_course.with_email('CUSTOMER1@TEST.COM') | |
end | |
def test_course_scope_and_finder_2 | |
assert_nil Customer.no_course.first(email: 'CUSTOMER1@TEST.COM') | |
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
source 'https://rubygems.org' | |
gem 'bson_ext' | |
gem 'mongo_mapper', '0.13.1' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment