Rspec:“电子邮件已被拍摄”

时间:2016-01-14 16:50:03

标签: ruby-on-rails ruby rspec

我正在为注册表单编写测试,我收到错误“已经收到电子邮件” 我已经搜索了这个问题并提出了这个宝石

gem 'database_cleaner', git: 'https://github.com/DatabaseCleaner/database_cleaner.git'

但它仍然没有修复错误 我可能搞砸了database_cleaner setup

spec_helper.rb

require 'database_cleaner'
Dir["./spec/support/**/*.rb"].sort.each { |f| require f}
RSpec.configure do |config|
  config.expect_with :rspec do |expectations|
    expectations.include_chain_clauses_in_custom_matcher_descriptions = true
    expectations.syntax = :should
  end


  config.mock_with :rspec do |mocks|

    mocks.verify_partial_doubles = true
  end

  config.before(:suite) do
    DatabaseCleaner[:active_record].strategy = :transaction
    DatabaseCleaner.clean_with(:truncation)
  end

  config.before(:each) do
    DatabaseCleaner.start
  end

  config.after(:each) do
    DatabaseCleaner.clean
  end
end

rails_helper.rb

ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'
require 'capybara/rspec'

ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|
  config.use_transactional_fixtures = true
  config.infer_spec_type_from_file_location!
  config.filter_rails_from_backtrace!
end

factories.rb

FactoryGirl.define do
    factory :identity do |f|
        f.name Faker::Name.name
        f.email Faker::Internet.email
        f.password Faker::Internet.password(4,40)
    end
end

identity_spec.rb

it "Registration successfully" do
        user = FactoryGirl.create(:identity)
        visit(new_identity_path)
        fill_in('Name', :with => user.name)
        fill_in('Email', :with => user.email)
        fill_in('Password', :with => user.password)
        fill_in('Password confirmation', :with => user.password)
        click_button 'Register'
        page.should have_content("You'r successfully logged in")
    end

更新

it "Invalid password" do
    user = FactoryGirl.create(:identity)
    puts "USER Email: #{user.email}"
    visit('/login')
    fill_in('Email', :with => user.email)
    fill_in('Password', :with => "incorrect")
    click_button 'Login'
    page.should have_content("Invalid info")
end

it "Registration successfully" do
    puts "IDENTITY COUNT: #{Identity.count}"
    user = FactoryGirl.create(:identity)
    puts "USER Email: #{user.email}"
    # visit(new_identity_path)
    # fill_in('Name', :with => user.name)
    # fill_in('Email', :with => user.email)
    # fill_in('Password', :with => user.password)
    # fill_in('Password confirmation', :with => user.password)
    # click_button 'Register'
    # page.should have_content("You'r successfully logged in")
end

输出

USER Email: litzy.legros@rogahnskiles.net
.IDENTITY COUNT: 0
USER Email: litzy.legros@rogahnskiles.net
.

2 个答案:

答案 0 :(得分:2)

看起来你可能有轻微配置错误。

尝试将require 'database_cleaner'放入spec_helper.rb文件中。

然后在require 'spec_helper'文件中添加rails_helper.rb

如果没有解决问题,请在问题中加入其余spec_helper.rbrails_helper.rb个文件。

更新

据我所知,帮助文件中的一切看起来都很好。我在你的实现和我自己的实现之间看到的唯一区别是你已经定义了以下策略:

DatabaseCleaner[:active_record].strategy = :transaction

而我的只是:

DatabaseCleaner.strategy = :transaction

我认为这不是问题,但值得一试。

如果没有解决问题,您可以在规范测试中抛出几个puts语句并让我们知道输出吗?像这样:

puts "IDENTITY COUNT: #{Identity.count}"
user = FactoryGirl.create(:identity)
puts "USER EMAIL: #{user.email}"

这将让我们知道两件事:

  1. database_cleaner实际上无法正常工作(如果工作正常,则计数应为0)
  2. Faker每次使用时都使用相同的电子邮件地址。

答案 1 :(得分:0)

Capybara测试不应使用交易。使用以下内容关闭rails_helper. rb中的交易

config.use_transactional_fixtures = false

database_cleaner docs解释原因:

  

当在规范中创建的数据在被测试应用程序中不可见时,您通常会发现功能规范错误地使用事务而不是截断策略。

     

一个经常出现的例子是,在规范中创建用户后,该规范神秘地无法与用户一起登录。发生这种情况是因为用户是在一个数据库连接上的未提交事务内创建的,而登录尝试是使用单独的数据库连接进行的。由于事务隔离,此单独的数据库连接无法访问在第一个数据库连接上创建的未提交的用户数据。

相关问题