Expected false to be truthy
This message simply says that there is a assert
that is returning false instead of true; in the error he should tell you specifically in what proof the problem is.
Because of your code, it is most likely that the error is in the test "guardar nuevo cliente"
, in which you generate a new client but without giving more information (ie Cliente.new
) so it is very likely not being saved with cliente.save
due to some validation in the model Cliente
. So that you can save it, you must give information to all the attributes according to the validations that you have in the model.
undefined local variable or method 'msg'
In the "no repetir el nif"
test you have a variable (or method) msg
that you have not created, just in this line:
assert_not_equal(cliente1, cliente2, [msg])
You must create the variable before using it on that line, or assign the message directly (without using the variable):
test "no repetir el nif" do
cliente1 = Cliente.new
cliente2 = Cliente.new
assert_not_equal(cliente1, cliente2, "Error")
end
In your example you have [msg]
, as shown in the documentation, but that only indicates that the message is optional, that is, the brackets are not used.
That the client does not repeat himself comparing the nif
First you have to create and save a client with a specific nif
, and then verify another client with that same nif
; example:
test "no repetir el nif" do
cliente1 = Cliente.create!(nif: 'A48265169')
cliente2 = cliente1.dup
assert_not cliente2.valid?
end
Check that I can edit it
Instead of checking if you can save the record, you should verify the content of the attribute that you have changed after saving it ; example:
test "editar cliente" do
cliente = Cliente.take
cliente.nombre = 'new'
cliente.save
assert_equal 'new', cliente.nombre
end
Check that I can save it
You only need to provide the attributes so that the validation does not fail; example:
test "guardar nuevo cliente" do
cliente = Cliente.new(nif: 'A48265169', nombre: 'Mantisa')
assert cliente.save
end
BONUS
You can facilitate your tests (and make them more readable) if you use setup
to prepare them:
require 'test_helper'
class ClienteTest < ActiveSupport::TestCase
# setup se ejecuta antes de cada prueba
setup do
@cliente = Cliente.new(nif: 'A48265169', nombre: 'Mantisa')
end
test "guardar nuevo cliente" do
assert @cliente.save
end
test "no guardar un cliente sin nombre" do
@cliente.nombre = ''
assert_not @cliente.save
end
test "borrar cliente" do
@cliente.save
assert @cliente.destroy
end
test "no repetir el nif" do
@cliente.save
cliente = @cliente.dup
assert_not cliente.valid?
end
test "editar cliente" do
@cliente.save
@cliente.nombre = 'new'
@cliente.save
assert_equal 'new', @cliente.nombre
end
end