使用rspec时,mongoid上的电子邮件已经被误删了

时间:2013-08-01 06:14:55

标签: rspec factory-bot

我正在使用RSpec,Mongoid DB和FactoryGirl。以下是我的 lesson_spec.rb factories / 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

的工厂/ users.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命令后遇到以下故障:

捆绑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)>'

无法解决此问题。

3 个答案:

答案 0 :(得分:0)

您可以使用序列生成唯一的电子邮件:

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

替换

email "teacher2@example.com"

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

答案 1 :(得分:0)

Mongoid找不到具有以下ID User的{​​{1}},因为您在规范中使用51f9fb3479478f6fcb000002创建(未保存在数据库中)。

这里有两个解决方案:

  • 使用FactoryGirl.build(:user)创建用户(慢,需要在db中写入,但有时需要)
  • 存储FactoryGirl.create(:user)操作以返回您的内置用户

    User.stub(:找到).and_return(@user)

此外,更喜欢User.find语法而不是设置let块中的实例变量:

before

关于您描述的问题,请尝试以下方法:

let(:user) { FactoryGirl.build(:user) }

before do
  # ...
end

答案 2 :(得分:0)

我以更好的方式得到了我自己问题的答案。 我正在使用database cleaner gem来清理测试数据。

参考Avdi Grimm's blog,我在这里写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

这些步骤有助于在规范运行之前清理测试数据。