2

我正在尝试为我的网络应用程序设置登录测试。成功登录后,用户应重定向到仪表板。但是,在仪表板规范中,工厂女孩创建的用户无法登录。

但是如果我使用

attr = Factory.attributes_for(:user)
user = User.create(attr)

代替

user = Factory.create(:user)

有用。在这两种情况下,User.count 返回 1

宝石文件

group :development, :test do
  gem 'selenium-webdriver', "~> 2.16.0"
  gem "capybara-webkit", "0.12.1"
  gem "capybara", "1.1.2"
  gem "database_cleaner", "0.8.0"
  gem "rspec-rails", " 2.11.0"
  gem 'capybara-screenshot', "0.2.1"
  gem "factory_girl_rails", "1.2"
  gem 'guard-spork', '0.3.2'
  gem 'spork', '0.9.0'
end

仪表板规格.rb

it "should return 200 and redirect to dashboard if user is already signed in" do
  user = FactoryGirl.create(:user, :password => "password", :password_confirmation => "password")

  puts user.count  # 1

  visit "/users/sign_in"

  # log in user
  fill_in "user_email", :with => user.email
  fill_in "user_password", :with => "password"
  click_on "Sign in"

  page.status_code.should == 200
  current_path.should eq("/u/dashboard")
end

rspec_helper.rb

require 'rubygems'
require 'spork'

Spork.prefork do
  ENV["RAILS_ENV"] ||= 'test'
  require File.expand_path("../../config/environment", __FILE__)

  require 'rspec/rails'
  require 'capybara/rspec'
  require 'capybara/rails'

  Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

  RSpec.configure do |config|

    config.mock_with :rspec

    # Cleaner
    DatabaseCleaner[:mongoid].strategy = :truncation
    config.before(:each) { DatabaseCleaner[:mongoid].clean }
    config.after(:each) { DatabaseCleaner[:mongoid].clean }

  end

  Capybara.javascript_driver = :webkit

end

Spork.each_run {}
4

0 回答 0