factory girl - Email already taken error on mongoid while using rspec -


i using rspec, mongoid db , factorygirl. following lesson_spec.rb , factories/users.rb.

require 'spec_helper' describe lesson   context "has valid data"     before :each       @course_template = factorygirl.create(:course_template1)       @user = factorygirl.build(:user)       @course_template.share_with(@user)       @lesson = factorygirl.create(:lesson)       @course_template.add_lesson(@lesson)       @course_instance = factorygirl.create(:course_instance)       @course_template.add_course_instance(@course_instance)     end      "has course instances"       p @lessons       @lessons.should respond_to :course_templates     end    end end 

factories/users.rb

factorygirl.define   factory :user_lesson, class: user     first_name 'lesson'     last_name 'user'     roles ["teacher"]     email "teacher2@example.com"     password '12345678'     password_confirmation '12345678'   end end 

i getting following failure while running following rspec command 2nd time onwards:

bundle exec rspec spec/models/lesson_spec.rb

1) lesson has valid data has course instances      failure/error: @course_template.share_with(@user)      mongoid::errors::documentnotfound:         problem:          document(s) not found class user id(s) 51f9fb3479478f6fcb000002.        summary:          when calling user.find id or array of ids, each parameter must match document in database or error raised. search id(s): 51f9fb3479478f6fcb000002 ... (1 total) , following ids not found: 51f9fb3479478f6fcb000002.        resolution:          search id in database or set mongoid.raise_not_found_error configuration option false, cause nil returned instead of raising error when searching single id, or matched documents when searching multiples.      # ./lib/shared.rb:29:in `share_new_asset_with'      # ./lib/shared.rb:51:in `update_or_share_with'      # ./lib/shared.rb:57:in `share_with'      # ./spec/models/lesson_spec.rb:9:in `block (3 levels) in <top (required)>' 

unable solve problem.

you use sequences generate unique emails:

https://github.com/thoughtbot/factory_girl/wiki/usage#sequences-and-associations

replace your

email "teacher2@example.com" 

with

sequence(:email) { |n| "teacher#{ n }@example.com } 

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -