Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails:Shoulda在更新时匹配并验证用户名_Ruby On Rails_Tdd_Rspec Rails_Shoulda - Fatal编程技术网

Ruby on rails Rails:Shoulda在更新时匹配并验证用户名

Ruby on rails Rails:Shoulda在更新时匹配并验证用户名,ruby-on-rails,tdd,rspec-rails,shoulda,Ruby On Rails,Tdd,Rspec Rails,Shoulda,模型验证: validates :username, uniqueness: true, format: { with: /\A[a-zA-Z0-9_.-@]+\Z/i, message: "must contain only letters, numbers or _*-@" }, on: :update, :if => :username_changed? Rspec: require 'spec_helper' describe User, "references" do i

模型验证:

validates :username, uniqueness: true, format: { with: /\A[a-zA-Z0-9_.-@]+\Z/i, message: "must contain only letters, numbers or _*-@" }, on: :update, :if => :username_changed?
Rspec:

require 'spec_helper'

describe User, "references" do
  it { should have_and_belong_to_many(:roles) }
  it { should belong_to(:account_type) }
  it { should belong_to(:primary_sport).class_name("Sport") }
  it { should belong_to(:school) }
  it { should belong_to(:city) }
  it { should validate_presence_of(:email) }
  it { should validate_uniqueness_of(:email) }
  it { should allow_value("test@test.com").for(:email) }
  it { should_not allow_value("test.com").for(:email) }

  describe "validation of username", focus: true do
    before(:each) do
      @user = User.new(email: Faker::Internet.email, password: "password", password_confirmation: "password", username: "test123", agreed_to_age_requirements: true)
    end

    it "should be valid" do
      @user.save
      @user.should be_valid
    end

    it "should not be valid with incorrect characters in username" do
      @user.username = "test@@@!!!"
      @user.should_not be_valid
    end
  end
end
工厂女工:

FactoryGirl.define do
  factory :user do
    email Faker::Internet.email
    password "password"
    password_confirmation "password"
    agreed_to_age_requirements true
    username Faker::Internet.user_name
  end 
end

基本上,我只是想测试自定义验证用户名的唯一性和指定格式的唯一性

您的问题是什么?使用Rspec,我将如何编写一个测试来测试:
validates:username,university:true,format:{with://\a[a-zA-Z0-9.-@]+\Z/I,消息:“必须只包含字母、数字或*-@”,on::update,:if=>:username\u changed?
@Vimsha-我用最近一次编写测试的尝试更新了我的问题