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

Ruby on rails 工厂女孩正在创建一些奇怪的资源条目,ruby-on-rails,rspec,capybara,factory-bot,guard,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

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
FactoryGirl.define do
    factory :user do
        email "example@hotmail.com"
        password "password"
    end
end
class Create < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :email
      t.string :password

      t.timestamps
    end
  end
end
require 'spec_helper'

describe "PasswordResets" do
    it "emails user when requesting a password rest" do
        user = FactoryGirl.create(:user)
        visit "/users"
    end
end
工厂。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
FactoryGirl.define do
    factory :user do
        email "example@hotmail.com"
        password "password"
    end
end
class Create < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :email
      t.string :password

      t.timestamps
    end
  end
end
require 'spec_helper'

describe "PasswordResets" do
    it "emails user when requesting a password rest" do
        user = FactoryGirl.create(:user)
        visit "/users"
    end
end
####u create.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
FactoryGirl.define do
    factory :user do
        email "example@hotmail.com"
        password "password"
    end
end
class Create < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :email
      t.string :password

      t.timestamps
    end
  end
end
require 'spec_helper'

describe "PasswordResets" do
    it "emails user when requesting a password rest" do
        user = FactoryGirl.create(:user)
        visit "/users"
    end
end
好的,这里有我的spec助手,我的工厂,我的数据库迁移来创建用户实例,最重要的是,密码重置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”从哪里来?太奇怪了。

听起来你需要在这里进行一些基本的调试。在FactoryGirl创建之后和访问之后,尝试打印用户表的内容。确保事实上是factory girl做错了什么,而不是您的代码。测试后似乎没有清理(删除)任何记录。也许这会给你一个好建议,但这是一个非常奇怪的错误。总是11个条目,没有失败。真奇怪。