Generate ruby tests

1

Trying to generate test in Ruby , specifically in my file _client_test.rb_ like this:

require 'test_helper'

class ClienteTest < ActiveSupport::TestCase
  test "no guardar un cliente sin nombre" do
    cliente = Cliente.new
    assert_not cliente.save
   end

  test "borrar cliente" do
    cliente = Cliente.take
    assert cliente.destroy
  end

 test "no repetir el nif" do 
    cliente1 = Cliente.new
    cliente2 = Cliente.new
    assert_not_equal(cliente1, cliente2, [msg])
 end

  test "guardar nuevo cliente" do
    cliente = Cliente.new
    assert cliente.save
  end

  test "editar cliente" do
    cliente = Cliente.take
    cliente.nombre = 'new'
    assert cliente.save
  end
end

Only the test of the first two pass me: not save a client without a name and delete client. In the others or I get the following error:

  

Expected false to be truthy

Or tell me in this case:

  

undefined local variable or method 'msg'

Does anyone know what is owed and how to solve it? I would like to actually check 3 things:

  • That the client does not repeat itself comparing the nif ,
  • Check that I can edit it and
  • Check that I can save it.
  • Thank you very much for your attention. Greetings.

        
    asked by mantisa 30.07.2018 в 17:51
    source

    1 answer

    2
      

    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
    
        
    answered by 30.07.2018 / 20:32
    source