简体   繁体   English

Rails,RSpec单元测试的用户名失败

[英]Rails, RSpec unit test failing for username

total n00b here. 总共n00b个。 I'm trying to figure out how to write unit tests for my user model. 我试图弄清楚如何为我的用户模型编写单元测试。 It's currently failing for 4 of my tests and I'm having a hell of a time figuring out why. 目前我的4项测试都失败了,我真费劲地弄清楚原因。

The first failure is for this line... 第一条失败是针对这条线...

it { should validate_uniqueness_of(:username) } fails with rails responding... it { should validate_uniqueness_of(:username) }因Rails响应而失败...

1) User should require case sensitive unique value for username
     Failure/Error: it { should validate_uniqueness_of(:username) }

     ActiveRecord::StatementInvalid:
       PG::NotNullViolation: ERROR:  null value in column "seeking_coach" violates not-null constraint
       DETAIL:  Failing row contains (1, , , , , a, , , , , null, null, null, null, null, null, 0, null, null, null, null, 2016-10-30 19:17:07.366431, 2016-10-30 19:17:07.366431, f, null, null, null).
       : INSERT INTO "users" ("username", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id"

Why is this unit test of username failing due to a null field while the prior unit test ( it { should have_valid(:username).when('Groucho', 'Marx')} ) passes? 为什么在先前的单元测试( it { should have_valid(:username).when('Groucho', 'Marx')} )通过时it { should have_valid(:username).when('Groucho', 'Marx')}单元测试it { should have_valid(:username).when('Groucho', 'Marx')}由于空字段而失败?

spec/models/user_spec.rb 规格/型号/user_spec.rb

  it { should have_valid(:username).when('Groucho', 'Marx')}
  it { should_not have_valid(:username).when(nil, '')}
  it { should validate_uniqueness_of(:username) }

models/user.rb 型号/user.rb

  validates :username, uniqueness: true, presence: true
  validates_format_of :username, with: /\A[a-zA-Z0-9]+\z/

Ok, having read around it appears this may actually be a bug related to the shoulda matcher for some edge case when testing for uniqueness with a NOT NULL database constraint on a different column. 好的,已经阅读了一下,看来当在另一列上使用NOT NULL数据库约束进行唯一性测试时,对于某些边缘情况,这实际上可能是与shoulda匹配器相关的错误。

https://github.com/thoughtbot/shoulda-matchers/issues/600 https://github.com/thoughtbot/shoulda-matchers/issues/600

As a workaround I would suggest that you explicitly set up a valid model and allow the shoulda matcher to do its own thing on the attribute to be tested. 作为一种解决方法,我建议您显式设置一个有效的模型,并允许shoulda匹配器在要测试的属性上做自己的事情。 You can set the subject that all shoulda matchers should use like so: 您可以这样设置所有匹配器应使用的主题:

describe User do
  subject { User.new(
    username: 'username',
    seeking_coach: 'coach',
    #
    # set other valid attributes here
    #
  )}

  it { should have_valid(:username).when('Groucho', 'Marx')}
  it { should_not have_valid(:username).when(nil, '')}
  it { should validate_uniqueness_of(:username) }
end

Of course if you are using FactoryGirl, and have a user factory that can build a valid user, then you can simply use: 当然,如果您使用的是FactoryGirl,并且拥有可以建立有效用户的用户工厂,则可以简单地使用:

subject { FactoryGirl.build(:user) }

Now all the shoulda matcher tests will use that object to run the tests with, and you shouldn't get database constraint issues for an attribute you aren't testing. 现在,所有的Shoulda Matcher测试都将使用该对象来运行测试,并且您不应该为未测试的属性遇到数据库约束问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM