FactoryGirl + Rspec + Rails3失败

时间:2014-10-21 17:07:03

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

我有来自我大学的这个项目,我正在尝试在做出任何更改之前理解代码。

代码有测试,这使得这更容易。但是我在运行这些测试时遇到了问题。编写de代码的人说它工作得很好,所以我认为这与宝石或类似东西有某种不相容之处。该项目有一年多没有更新,这就是为什么宝石可能有点老了。但由于我没有改变任何宝石,这应该有效。让我们来解决问题:

这是我的测试:

it 'edit user information' do
  user = FactoryGirl.create :user
  login(user.email, '123456')
  click_link user.email
  fill_in 'E-mail', :with => 'another_email@email.com'
  click_button 'Save'
  page.should have_content 'Update successfull.'
end

这是我的登录功能,位于spec_helper

def login(email, password)
  visit '/admin'
  fill_in('user_email', :with => email)
  fill_in('user_password', :with => password)
  click_button "Entrar"
end

这就是错误:

1) login and register edit user information
   Failure/Error: login(user.email, '123456')
   ArgumentError:
     wrong number of arguments (1 for 0)
   # ./spec/acceptance/login_spec.rb:18:in `block (2 levels) in <top (required)>'

第18行是:login(user.email,'123456')

过了一会儿,我才意识到:

  • 如果我将字符串作为参数登录到登录功能,例如login('login@email.com', '123456')而不是login(user.email, '123456')。它运行,但我收到另一个错误,因为登录@ email在我的数据库中不存在然后我无法登录。
  • 但是,如果此字符串与FactoryGirl生成的字符串完全相同,则会再次收到参数错误。

我已经尝试更新几乎所有的gemfiles,但它没有成功。所以,在我解决这个问题之前,我仍在使用旧宝石。这是我的gemfile:

source 'http://rubygems.org'

gem 'rails', '3.2.11'
gem 'rake', '10.1.0'
gem 'mysql2', '0.3.11'
gem 'devise', '2.2.3'
gem 'fastercsv', '1.5.5'
gem 'rails_admin', '0.4.3'
gem 'cancan', '1.6.9'
gem 'simple_form', '2.0.4'
gem 'slim', '1.3.6'
gem 'jquery-rails', '2.2.0'
gem 'sprockets', '~> 2.2.1'
gem 'ckeditor', '4.0.2'
gem 'paperclip', '3.4.0'
gem 'twitter-bootstrap-rails', '2.2.0'
gem 'bootstrap-wysihtml5-rails', '0.3.1.17'
gem 'less-rails', '2.2.6'
gem 'therubyracer', '~> 0.12.0'
gem 'kaminari', '0.14.1'

group :assets do
  gem 'uglifier', '1.3.0'
  gem 'yui-compressor', '0.9.6'
end

group :test do
  gem 'rspec-rails', '2.11.0'
  gem 'capybara', '~> 2.0.2'
  gem 'launchy', '2.1.2'
  gem 'factory_girl_rails', '1.6.0'
  gem 'valid_attribute', '1.3.1'
  gem 'spork', '0.9.2'
  gem 'capybara-webkit', '1.1.1'
  gem 'database_cleaner', '0.8.0'
  gem 'shoulda-matchers'
  gem 'autotest'
end

group :developmet do
  gem 'rails3-generators'
  gem 'slim-rails', '1.1.0'
  gem 'thin', '1.5.0'
end

任何人都可以帮我解决这个问题吗?

编辑:

工厂用户:

# -*- encoding : utf-8 -*-
FactoryGirl.define do
  factory :user do
    sequence(:email) { |n| "user#{n}@email.com" }
    password "123456"
    password_confirmation "123456"
    admin true
  end
end

spec_helper.rb:

# -*- encoding : utf-8 -*-
require 'rubygems'
require 'spork'

Spork.prefork do
  # 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 'capybara/rspec'
  require 'valid_attribute'
  require 'cancan/matchers'
  require 'paperclip/matchers'

  IMAGE = File.expand_path("../data/image.jpg", __FILE__)
  TEXT = File.expand_path("../data/text.txt", __FILE__)

  # 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}

  Capybara.javascript_driver = :webkit

  RSpec.configure do |config|
    config.mock_with :rspec
    config.use_transactional_fixtures = false
    config.include Paperclip::Shoulda::Matchers

    config.before :each do
      if example.metadata[:js]
        Capybara.server_port = 33333
        Capybara.current_driver = :webkit
      end
      if Capybara.current_driver == :rack_test
        DatabaseCleaner.strategy = :transaction
      else
        DatabaseCleaner.strategy = :truncation
      end
      DatabaseCleaner.start
    end

    config.after do
      DatabaseCleaner.clean
      Capybara.use_default_driver if example.metadata[:js]
    end
  end

  def login(email, password)
    visit '/admin'
    fill_in('user_email', :with => email)
    fill_in('user_password', :with => password)
    click_button 'Entrar'
  end
end

Spork.each_run do
  require File.expand_path("../../config/routes", __FILE__)
  load "#{Rails.root}/config/routes.rb"
  Dir["#{Rails.root}/app/**/*.rb"].each { |f| load f }
end

login_spec.rb:

# -*- encoding : utf-8 -*-
require 'spec_helper'

feature 'login and register' do
    background do
        FactoryGirl.create :configuration
    end

    it 'should not be possible guest user register an administrator' do
        visit '/admin'
        page.should_not have_content 'Registrar'

        lambda { visit '/users/sign_up' }.should raise_error ActionController::RoutingError
    end

    it 'edit user information' do
        user = FactoryGirl.create :user
        login(user.email, '123456')
        click_link user.email
        fill_in 'E-mail', :with => 'outro_email@email.com'
        click_button 'Salvar'
        page.should have_content 'Usuário atualizado(a) com sucesso.'
    end
end

0 个答案:

没有答案