如何测试(minitest)获取Rails中的语言环境

时间:2016-06-08 19:30:36

标签: ruby-on-rails routes minitest

我试图找出如何测试(minitest)具有区域设置并且在范围内的路线:

  scope ":locale", locale: /#{I18n.available_locales.join("|")}/ do
    get 'static_pages/about'
    resources :salas    
  end

这不起作用:

require 'test_helper'

class StaticPagesControllerTest < ActionController::TestCase
  test "should get about" do
    get :about
    assert_response :success
  end

end

并将产生以下输出:

# Running:

E

Finished in 0.301302s, 3.3189 runs/s, 0.0000 assertions/s.

  1) Error:
StaticPagesControllerTest#test_should_get_about:
ActionController::UrlGenerationError: No route matches {:action=>"about", :controller=>"static_pages"}
    test/controllers/static_pages_controller_test.rb:5:in `block in <class:StaticPagesControllerTest>'

1 runs, 0 assertions, 0 failures, 1 errors, 0 skips

rake routes:

        Prefix Verb   URI Pattern                       

    Controller#Action
static_pages_about GET    /:locale/static_pages/about(.:format) static_pages#about {:locale=>/en|es/}
             salas GET    /:locale/salas(.:format)              salas#index {:locale=>/en|es/}
                   POST   /:locale/salas(.:format)              salas#create {:locale=>/en|es/}
          new_sala GET    /:locale/salas/new(.:format)          salas#new {:locale=>/en|es/}
         edit_sala GET    /:locale/salas/:id/edit(.:format)     salas#edit {:locale=>/en|es/}
              sala GET    /:locale/salas/:id(.:format)          salas#show {:locale=>/en|es/}
                   PATCH  /:locale/salas/:id(.:format)          salas#update {:locale=>/en|es/}
                   PUT    /:locale/salas/:id(.:format)          salas#update {:locale=>/en|es/}
                   DELETE /:locale/salas/:id(.:format)          salas#destroy {:locale=>/en|es/}
                   GET    /*path(.:format)                      redirect(301, /en/%{path})
              root GET    /                                     salas#index

我想也许我需要将语言环境传递给测试,但我不确定如何。 谢谢!

1 个答案:

答案 0 :(得分:0)

略低于此标题:Rails指南中的Function Tests for your Controllers:测试Rails指南显示了get方法的约定。要将params传递给方法,请将其作为调用的第二个参数。

get :about, locale: 'en'