0

规范助手.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

工厂.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”是从哪里来的?太奇怪了。

4

0 回答 0