Home>

I have a question.
I am currently writing test code on ruby ​​on rails.
Although it is not an error, the following phenomenon occurred.
This is the test code of the user model using RSpecwo. (Using devise)

Here is the test code for the problem.
Since the nickname is empty on the 25th line, an error normally occurs, but the test code passes normally.
[Spec>models>user_spec.rb]

describe User do
  before do
    @user = FactoryBot.build (: user)
  end
  describe'new user registration' do
    context'When new registration is successful' do
      it "You can register if nickname and email, password and password_confirmation exist" do
        expect (@user) .to be_valid
      end
      it "nickname can be registered with 8 characters or less" do
        @ user.nickname = "aaaaaaaa"
        expect (@user) .to be_valid
      end
      it "You can register if your password is 6 characters or more" do
        @ user.password = "000000"
        @ user.password_confirmation = "000000"
        expect (@user) .to be_valid
      end
    end
    context'When new registration does not work' do
      it "Cannot register if nickname is empty" do
        @ user.nickname =''
        @ user.valid?
      end
      it "Cannot register if nickname is 9 characters or more" do
        @ user.nickname ='aaaaabccccccccccccccccccbbb'
        @ user.valid?
      end
    end
  end
end
code

I've validated the nickname column with presence: true on line 10 as shown below, but the test passes successfully.
[App>models>users.rb]

#Include default devise modules. Others available are:
  #: confirmable,: lockable,: timeoutable,: trackable and: omniauthable
  devise: database_authenticatable,: registerable,
         : recoverable,: rememberable,: validatable
  has_many: nutritions
  has_many: favorites, dependent:: destroy
  has_many: fav_nutritions, through:: favorites, source:: nutrition
  validates: nickname, presence: true, length: {maximum: 8}
  def already_favorited? (nutrition, current_user)
    Favorite.exists? (user_id: current_user.id, nutrition_id: nutrition.id)
  end
end
code

↓ I test it with the bundle exec rspec spec/models/user_spec.rb command, but for some reason it succeeds.

Normally, I think that an error will occur when the nickname is empty. Why?
The local browser has been validated normally. (An error message is returned when trying to register niskname empty)

I would appreciate it if you could teach me.