【问题标题】:ROR - rails - rspec - factory_girl should_not not working (as expected)ROR - rails - rspec - factory_girl should_not 不工作(如预期的那样)
【发布时间】:2014-03-10 15:20:53
【问题描述】:

问题是我预计第二次 it 会失败,第三次会通过,但情况恰恰相反。 2通过,3失败。有人知道为什么吗?

spec/models/user_spec.rb

require 'spec_helper'

describe User do
  it 'has a valid factory' do
    FactoryGirl.create(:user).should be_valid
  end
  it 'is invalid with empty email1' do
    # I've also tried
    # FactoryGirl.build(:user, email: nil, validation_scenario: 'create').should be_valid
    FactoryGirl.build(:user, email: nil).should be_valid
  end
  it 'is invalid with empty email2' do
    FactoryGirl.build(:user, email: nil).should_not be_valid
  end
end

spec/factories/user.rb

require 'faker'

FactoryGirl.define do
  factory :user do |u|
    u.email                 { Faker::Internet.email }
    u.password              'password'
    u.password_confirmation 'password'
  end
end

models/user.rb

class User < ActiveRecord::Base

  attr_accessor :password, :validation_scenario

  # validate on user create/update
  with_options if: -> user { ['create', 'update'].include? user.validation_scenario } do |user|
    user.validates_confirmation_of :password
    user.validates_presence_of :password, :on => :before_create
    user.validates_presence_of :email, :password
    user.validates_uniqueness_of :email
    user.validates_format_of :email, :with =>/.+@.+\..+/i
  end

  # validate on resend email cofirmation/reset password
  with_options if: -> user { ['send_email_confirmation', 'reset_password'].include? user.validation_scenario } do |user|
    user.validates_presence_of :email
    user.validates_format_of :email, :with =>/.+@.+\..+/i
  end
  ...

控制器/user_controller.rb

class UsersController < ApplicationController
  def create
    @user = User.new(user_params)
    @user.validation_scenario = 'create'

    respond_to do |format|
      if @user.save
        format.html { redirect_to root_url, notice: 'User was successfully created.' }
      else
        format.html { render action: 'new' }
      end
    end
  end
  def user_params
    params.require(:user).permit(:email, :password, :password_confirmation)
  end
end

我在这里学习本教程: http://everydayrails.com/2012/03/19/testing-series-rspec-models-factory-girl.html

ruby 2.1.0p0(2013-12-25 修订版 44422)[x86_64-linux]

Rails 4.0.2

【问题讨论】:

  • user.validation_scenario 是如何设置的?
  • 在控制器动作中(手动)。但我也尝试在测试文件中设置它spec/models/user_spec.rb
  • 你能分享那个代码吗?
  • 如果你想看看它是如何实现的,我可以分享代码,但它与unit tests 没有任何关系。谢谢你,我已经解决了:)(谢谢)我在密码确认后在spec/factories/user.rb 中添加了一个新字段:u.validation_scenario 'create',它就像一个魅力。
  • 在我看来,您根本没有调用自定义验证。

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


【解决方案1】:

问题是我没有在任何地方设置validation_scenario。所以现在:

spec/factories/user.rb 看起来像这样:

require 'faker'

FactoryGirl.define do
  factory :user do |u|
    u.email                 { Faker::Internet.email }
    u.password              'password'
    u.password_confirmation 'password'
  end

  factory :create_user, parent: :user do |u|
    u.validation_scenario   'create'
  end
end

和spec/model/user_spec.rb

require 'spec_helper'

describe User do
  it 'has a valid factory' do
    FactoryGirl.create(:create_user).should be_valid
  end
  it 'is invalid with empty email' do
    FactoryGirl.build(:create_user, email: nil).should_not be_valid
  end
  it 'is invalid with empty password' do
    FactoryGirl.build(:create_user, password: nil).should_not be_valid
  end
  it 'is invalid with empty password_confirmation' do
    FactoryGirl.build(:create_user, password: nil).should_not be_valid
  end
end

像魅力一样工作

【讨论】:

    猜你喜欢
    • 2014-05-16
    • 1970-01-01
    • 2012-06-13
    • 2018-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-16
    • 2016-03-07
    相关资源
    最近更新 更多