0

在我的 Rails 项目中,我尝试使用设计 gem 从电子邮件登录切换到用户名登录。到目前为止,我做了所有步骤,但是当我做黄瓜时,我看到了这个输出:

Given a user "Aslak" exists         
# features/step_definitions/login_steps.rb:16
  unknown attribute: username (ActiveRecord::UnknownAttributeError)
  ./features/step_definitions/login_steps.rb:17:in `/^a user "(.*?)" exists$/'
  features/login.feature:4:in `Given a user "Aslak" exists'

给定用户“Aslak” 的存在如下所示:

Given /^a user "(.*?)" exists$/ do |user_name|
  @user = User.create!(:username => user_name, :password => "s3cr3t")
end

但是当我做 rails c

u = User.first 

我看到这个输出:

=> #<User id: 1, email: "aslak@tamarawobben.nl", encrypted_password: "$2a$10$XjYgPtD4Ol4/sEpe934O6.yPcxs.jBF.m9SNTV.qhJp4...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 3, current_sign_in_at: "2012-11-05 20:27:44", last_sign_in_at: "2012-11-05 19:35:00", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", created_at: "2012-11-03 20:21:54", updated_at: "2012-11-06 07:58:36", username: "asLak">

所以在我看来,用户名是存在的。

有人知道如何解决这个问题吗?

罗洛夫

4

2 回答 2

0

似乎您需要不区分大小写进行比较,而 ruby​​ 默认情况下比较区分大小写。这应该可以解决您的问题:

Given /^a user "(.*?)" exists$/ do |user_name|
    @user = User.create!(:username => user_name.downcase! , :password => "s3cr3t")
end

您是否在运行测试之前运行迁移(如下)?

env RAILS_ENV=test rake db:migrate
于 2012-11-06T18:31:35.640 回答
0

您是否有任何机会编辑了现有迁移并忘记运行rake db:test:clone以将更改传播到测试数据库?

rails c在开发中运行,它使用另一个数据库。改为检查User.firstafter 运行的结果。rails c test

于 2012-11-06T21:32:37.583 回答