我正在使用 RSpec、Mongoid DB 和 FactoryGirl。以下是我的course_spec.rb和factory/users.rb。
require 'spec_helper'
describe Lesson do
context "has valid data" do
before :each do
@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
it "has course instances" do
p @lessons
@lessons.should respond_to :course_templates
end
end
end
工厂/用户.rb
FactoryGirl.define do
factory :user_lesson, class: User do
first_name 'Lesson'
last_name 'User'
roles ["teacher"]
email "teacher2@example.com"
password '12345678'
password_confirmation '12345678'
end
end
我在第二次运行以下 rspec 命令时遇到以下故障:
bundle exec rspec 规范/模型/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 for class User with id(s) 51f9fb3479478f6fcb000002.
Summary:
When calling User.find with an id or array of ids, each parameter must match a document in the database or this error will be raised. The search was for the id(s): 51f9fb3479478f6fcb000002 ... (1 total) and the following ids were not found: 51f9fb3479478f6fcb000002.
Resolution:
Search for an id that is in the database or set the Mongoid.raise_not_found_error configuration option to false, which will cause a nil to be returned instead of raising this error when searching for a single id, or only the matched documents when searching for 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)>'
无法解决这个问题。