使用rspec时mongoid上的电子邮件已出错
我正在使用RSpec、Mongoid DB和FactoryGirl。以下是我的课程\u spec.rb和工厂/用户.rb使用rspec时mongoid上的电子邮件已出错,rspec,factory-bot,Rspec,Factory Bot,我正在使用RSpec、Mongoid DB和FactoryGirl。以下是我的课程\u spec.rb和工厂/用户.rb require 'spec_helper' describe Lesson do context "has valid data" do before :each do @course_template = FactoryGirl.create(:course_template1) @user = FactoryGirl.build(:us
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
factories/users.rb
我在第二次运行以下rspec命令时出现以下故障:
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 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)>'
1)课程有有效数据,有课程实例
失败/错误:@course\u模板。与(@user)共享
Mongoid::错误::DocumentNotFound:
问题:
找不到id为51f9fb3479478f6fcb000002的类用户的文档。
总结:
使用id或id数组调用User.find时,每个参数必须与数据库中的文档匹配,否则将引发此错误。搜索的是id:51f9fb3479478f6fcb000002。。。(共1个)未找到以下ID:51f9fb3479478f6fcb000002。
决议:
搜索数据库中的id或将Mongoid.raise\u not\u found\u error configuration选项设置为false,这将导致在搜索单个id时返回nil而不是引发此错误,或者在搜索多个id时仅返回匹配的文档。
#./lib/shared.rb:29:in'share\u new\u asset\u with'
#./lib/shared.rb:51:in'update_或_shared_'
#/lib/shared.rb:57:in'share_with'
#./spec/models/lesson_spec.rb:9:in'block(3层)in'
无法解决此问题。您可以使用序列生成唯一的电子邮件: 替换你的
email "teacher2@example.com"
与
Mongoid无法找到id为51f9fb3479478f6fcb000002的
用户
,因为您是使用FactoryGirl.build创建的(未保存在数据库中)
这里有两种解决方案:
- 使用FactoryGirl创建用户。创建(:user)
(速度慢,需要在db中写入,但有时需要)
用户。查找操作以返回生成的用户
User.stub(:find.)和_return(@User)
块之前的中设置实例变量相比,更喜欢let
语法:
let(:user) { FactoryGirl.build(:user) }
before do
# ...
end
关于您描述的问题,请尝试以下方法:
FactoryGirl.define do
sequence :email do |n|
"foo#{n}@bar.com"
end
factory :user do
email
end
end
我以更好的方式得到了自己问题的答案。
我正在使用清理测试数据
关于,我在这里为MongoDB ORM写作
#spec/spec_helper.rb
config.use_transactional_fixtures = false
这将禁用rspec rails在数据库事务中隐式包装测试
#spec/support/database_cleaner.rb
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner[:mongoid].strategy = :truncation
end
config.before(:each, :js => true) do
DatabaseCleaner[:mongoid].strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
这些步骤有助于在运行规范之前清理测试数据。仍然会出现相同的错误。它不会创建唯一的电子邮件ID。有什么问题吗?谢谢你的回答。但我在这里找到了更好的写作方法。
#spec/spec_helper.rb
config.use_transactional_fixtures = false
#spec/support/database_cleaner.rb
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner[:mongoid].strategy = :truncation
end
config.before(:each, :js => true) do
DatabaseCleaner[:mongoid].strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end