工厂女孩正在创建一些奇怪的资源条目

时间:2013-09-26 04:37:54

标签: ruby-on-rails rspec capybara factory-bot guard

spec helper.rb

# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rails'
require 'capybara/rspec'
require 'factory_girl_rails'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)

RSpec.configure do |config|

end

factories.rb

FactoryGirl.define do
    factory :user do
        email "example@hotmail.com"
        password "password"
    end
end

### _ create.rb

class Create < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :email
      t.string :password

      t.timestamps
    end
  end
end

password_resets_spec.rb

require 'spec_helper'

describe "PasswordResets" do
    it "emails user when requesting a password rest" do
        user = FactoryGirl.create(:user)
        visit "/users"
    end
end

好的,有我的规范助手,我的工厂,我的数据库迁移来创建用户实例,最重要的是,password_resets_spec.rb

运行它时,我希望在测试用户表中创建一个用户实例:

id | email | password | created_at | updated_at

1 | example@hotmail.com | password | 2013-12-13 04-32-12 | 2013-12-13 04-32-12 |

对于水豚来说,浏览现有的路线,并且不会发生任何失败。

我曾经跑过一次,虽然我没有失败,但是rspec已经完成了23个例子。这就是我的表格中发生的事情:

    id | email | password | created_at | updated_at

    1 | MyString |    | 2013-12-13 04-32-12 | 2013-12-13 04-32-12 |
    2 | MyString |    | 2013-12-13 04-32-12 | 2013-12-13 04-32-12 |
    3 | MyString |    | 2013-12-13 04-32-12 | 2013-12-13 04-32-12 |
    4 | MyString |    | 2013-12-13 04-32-12 | 2013-12-13 04-32-12 |
    5 | MyString |    | 2013-12-13 04-32-12 | 2013-12-13 04-32-12 |
    6 | MyString |    | 2013-12-13 04-32-12 | 2013-12-13 04-32-12 |
    7 | MyString |    | 2013-12-13 04-32-12 | 2013-12-13 04-32-12 |
    8 | MyString |    | 2013-12-13 04-32-12 | 2013-12-13 04-32-12 |
    9 | MyString |    | 2013-12-13 04-32-12 | 2013-12-13 04-32-12 |
   10 | MyString |    | 2013-12-13 04-32-12 | 2013-12-13 04-32-12 |
   11 | MyString |    | 2013-12-13 04-32-12 | 2013-12-13 04-32-12 |

我第二次运行相同的测试,我得到了这个失败

UsersController GET index assigns all users as @users

该表现在是两倍大,有23个相同的'MyString'条目。到底是怎么回事?

如果我摆脱了

visit "/users"

一切都按预期工作。一切。那什么是水豚呢?为什么会创建11个“MyString”条目?哪里的“MyString”甚至来自哪里?太奇怪了。

0 个答案:

没有答案