XaoGao / Todoser

Clone trello
3 stars 11 forks source link

Flash service application controller #147

Closed markcodexq closed 2 years ago

XaoGao commented 2 years ago

@markcodexq Погоди, правильнее будет просто вмержить эту ветку в задачу, иначе будет сильное дробление(что в целом не плохо когда есть какой нибудь Продук, но в нашем случае слишком маленькая команда)

XaoGao commented 2 years ago

@markcodexq теперь необходимо добавить тесты для для данного concern, можно посмотреть пример создания фейкого контроллера в файле Todoser/spec/controllers/concerns/api_responders_spec.rb, пример теста для проверки flash

expect(flash[:success]).to match(/Some text/)
expect(flash[:alert]).to match(/Some error text/)
markcodexq commented 2 years ago
require 'rails_helper'

describe Resulteable, type :controller do
  controller(ApplicationController) do
    include ApiRespoders

    def render_object
      ok(token: "Bearer token")
    end
  end

  before do
    routes.draw do
      get :render_object, to: "anonymous#render_object"
    end
  end

  it 'success flash resultable' do
    get :render_object
    expect(flash[:success]).to match(/Some text/)
  end
end

Примерно такое?

XaoGao commented 2 years ago
require 'rails_helper'

describe Resulteable, type :controller do
  controller(ApplicationController) do
    include ApiRespoders

    def render_object
      ok(token: "Bearer token")
    end
  end

  before do
    routes.draw do
      get :render_object, to: "anonymous#render_object"
    end
  end

  it 'success flash resultable' do
    get :render_object
    expect(flash[:success]).to match(/Some text/)
  end
end

Примерно такое?

У тебя должно быть два метода у контроллера, так же у тебя не вызывается сам методо который тестируешь, должно быть что-то вроде

def success
  result = Result.new(true, "success object", nil)
  flash_service_result(result)
end
markcodexq commented 2 years ago
require 'rails_helper'

describe Resulteable, type: :controller do
  controller(ApplicationController) do
    include Resulteable

    def success
      result = Result.new(true, "success object", nil)
      flash_service_result(result)
    end
  end

  end

  it 'success flash resultable' do
    get :success
    expect(flash[:success]).to match(/Some Text/)
  end
end

не понимаю как вызвать в тесте метод success?

XaoGao commented 2 years ago

@markcodexq ты в тесте по факту создаешь новый контроллер

controller(ApplicationController) do

Добавляешь в него нужные тебе action

def success ...
def error ...

Затем ты добавляешь нужные тебе роуты для http запроса

before do
    routes.draw do
      get :success, to: "anonymous#success"
     # и путь для error
    end
  end

Затем внутри блока it делаешь запрос

get :success

и в итоге идет вызов нужного тебе метода контроллера, после вызова этого метода нужно проверить flash

markcodexq commented 2 years ago

Да я так сделал и пишет image

require 'rails_helper'

describe Resulteable, type: :controller do
  controller(ApplicationController) do
    include Resulteable

    def success
      result = Result.new(true, "success object", nil)
      flash_service_result(result)
    end
  end

  before do
    routes.draw do
      get :success, to: "anonymous#success"
    end
  end

  it 'success flash resultable' do
    get :success
    expect(flash[:success]).to match(/Some Text/)
  end

end
markcodexq commented 2 years ago

Всё запушил, внёс изменения, тесты прошли успешно

XaoGao commented 2 years ago

@markcodexq в целом все ок, есть замечанию по стилям, прошу исправить

markcodexq commented 2 years ago

Исправил

markcodexq commented 2 years ago

Теперь исправил с rubocop