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.
-
Answer # 1
Related articles
- ruby on rails - rspec of the like function how to get the number of likes or how to reflect by clicking the icon
- ruby on rails - rails tutorial tests that should pass fail with an error [6th edition]
- ruby on rails - it works fine on the browser, but the rspec test doesn't pass
- ruby on rails - rspec conditional validation
- ruby on rails - i want to write a test code that matches the rspec value
- ruby on rails - rails6 rspec integration test error
- ruby on rails - how to deal with status code 422 in the post request test with rspec
- ruby on rails - rspec validation error
- ruby on rails - i want to write a rails tutorial test in rspec
- ruby on rails 5 - rspec controller test doesn't work
- ruby on rails 5 - the routing that i should have set in rails5 resources is not working
- ruby on rails - rspec test changed by -1, but was changed by 0 error
- ruby on rails - about request spec of rspec about test code at login
- ruby on rails - rails rspec argumenterror occurs when the mock under test has arguments
- ruby - [rails] i want to change the argument of link_to using conditional branching
- ruby on rails - i want to resolve rails nameerror
- ruby on rails - i can't update rubygems
- ruby - about "activemodel :: unknownattributeerror" of rails
- ruby on rails 6 - rails6 activerecord :: recordinvalid in userscontroller # update
- ruby on rails - [rails] routing error occurs in devise_token_auth and the registration screen cannot be displayed
- ruby - the method defined in the model cannot be used
- ruby - about the problem of duplicate devise error messages (allow_blank: ture doesn't work!)
- ruby - [rspec] error in "dependent: destoy" test: how to resolve "validation failed"
- ruby - please tell me the solution of rails devise (given 0, expected 1)!
- ruby - i get a validation error when using factorybot with rspec
- ruby - no error message is displayed during new registration
- ruby - i can't install ffi with everyday rails setup
- ruby on rails - even though i can log in with rails, the current_user specified in the user details view becomes nil and an erro
nothing
expect
Isn't that because you haven't?