samedi 6 février 2016

How to test Controllers in Minitest?

I'm completely lost on trying to create tests and really need help. I have the following test for a controller api/v1/user_controller.rb (route is api_v1_user).

A couple questions:

1) How do I use a 'get users_path' or something similar to connect to the controller instead of having to go through the localhost and hitting the api?

2) Is there a way to clone the development server for running tests?

file: 'test/controllers/api/v1/user_controller_test.rb'

require File.expand_path("../../../../test_helper", __FILE__)

require 'minitest/autorun'
require 'minitest/reporters'
Minitest::Reporters.use!

class TestUser < Minitest::Test

  def test_show

    response = RestClient.get("http://localhost:3000/api/v1/users/1",
                              {
                                  "Content-Type" => "application/json",
                                  "Authorization" => ""
                              }
    )


    @data = JSON.parse response.body

    assert_equal @data["email"], "admin@email.com"

  end

end

Aucun commentaire:

Enregistrer un commentaire