I am following ruby on rails tutorial .
And I have few questions:
1.Hier it is showed that
>> "a" * 51
=> "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
so we try to compare if our user has a name of 51 “a”‘s and not of 51 charachters by typing this code:
before { @user.name = "a" * 51 }
It is showed as well that
(“a” * 51).length is 51.
so why dont we write
before { @user.name = (“a” * 51).length }
It is more logical, so that the user name consist of 51 charachters and not 51 a’s.
2.And as well this moment seems very strange. The explanation is as well odd.
describe "when email address is already taken" do
before do
1. user_with_same_email = @user.dup
2. user_with_same_email.save
end
it { should_not be_valid }
end
in 1. we duplicate our original user (for example foo=User.new(name:”Piks”, email:”piks@piks.com”) and put the copie of the foo in the variable user_with_same_email.
in 2. we save the copie of the foo and it returns us “true”, so that we have saved the copie and the original foo has the email address that already exists in the database and threfore should be invalid.
But how can the original foo can be invalid it I want to save it? It is not logically at all to save the copie of the user and the original user is then invalid.
I guess I misunderstand the “before block”. The “before block”, namely the method user_with_same_email.save should return “false”. Why?Because for instance, we have our database full of emails(there are as well logins) and then I try to register myself with the email that already exists in the database.
So, the system makes a copy of me and save it in the user_with_same_email and try to save it, but it cannot because another user with my email already exists in the database. So, the method user_with_same_email.save will return false and hence I am considered to be an invalid user.
In this very example the “before block” should return “true” in roder to proceed to it { should_not be_valid } or am I wrong?
I will appreciate if somebody helps me.
Thank you
You misunderstood the whole stuff. It is not the model code, it is a test for it. The tutorial tries to force test driven development. The idea is basically to specify statements about your system which are forced to be implemented, like “the name can’t be longer than 50 character”. You create a test for it where you try to break this statement, and if you can break it, then the system is faulty.
Look it like this:
One test is to set the user’s name to a string more than 50 character long. This is guaranteed to be that long. And it should be invalid, as it is. The test system will notify you if you forgot to add the length constraint in your model.
This is just another test. It creates a dummy user, duplicates it, and tries to save both. This should fail as well. If it fails the test is successful and it means your code is good.
One more thing, you asked why not just use
before { @user.name = ("a" * 51).length }. This is clearly invalid, you just try to set the user’s name to be 51, a number, not a string 51 characters long.