如何为包含区域设置的链接编写Rspec测试?

时间:2014-05-30 16:50:47

标签: ruby-on-rails rspec internationalization rspec-rails i18n-gem

我正在使用RSpec 2为带有本地化的Ruby on Rails 4.0.5应用程序编写测试。我正在使用Michael Hartl的Ruby on Rails教程中的示例。我试图重写测试,其中登录的人试图访问不同的人的页面。本教程中的示例不包括本地化。

describe "as wrong user" do
  let(:user) { FactoryGirl.create(:user) }
  let(:wrong_user) { FactoryGirl.create(:user, email: "wrong@example.com") }
  before { sign_in user, no_capybara: true }

  describe "submitting a GET request to the Users#edit action" do
    before { get edit_user_path(wrong_user) }
    specify { expect(response.body).not_to match(full_title('Edit user')) }
    specify { expect(response).to redirect_to(root_url) }
  end

  describe "submitting a PATCH request to the Users#update action" do
    before { patch user_path(wrong_user) }
    specify { expect(response).to redirect_to(root_url) }
  end
end

我的应用程序中的路径的链接看起来像http://example.con/locale而不是http://example.con/?locale=locale

我的测试在一个循环中。

I18n.available_locales.each do |locale|

  subject { page }

  all my tests...........

end

这是我尝试使用local_root_path重写我的测试:

describe "as wrong member" do

  let(:member) { FactoryGirl.create(:member) }
  let(:wrong_member) { FactoryGirl.create(:member, email: "wrong@example.com") }
  before { login member, no_capybara: true }

  describe "submitting a GET request to the Members#edit action" do
    before { get edit_member_path(wrong_member, locale: locale) }
    specify { expect(response.body).not_to match(full_title("#{wrong_member.first_name} #{wrong_member.last_name}")) }
    specify { expect(response).to redirect_to(locale_root_path) }
  end

  describe "submitting a PATCH request to the Members#update action" do
    before { patch member_path(wrong_member, locale: locale) }
    specify { expect(response).to redirect_to(locale_root_path) }
  end

end

运行测试时出现以下错误。

Failures:

  1) Authentication authorization as wrong member submitting a GET request to the Members#edit action 
     Failure/Error: specify { expect(response).to redirect_to(locale_root_path) }
       Expected response to be a redirect to <http://www.example.com/en> but was a redirect to <http://www.example.com/?locale=en>.
       Expected "http://www.example.com/en" to be === "http://www.example.com/?locale=en".
     # ./spec/requests/authentication_pages_spec.rb:101:in `block (6 levels) in <top (required)>'

  2) Authentication authorization as wrong member submitting a PATCH request to the Members#update action 
     Failure/Error: specify { expect(response).to redirect_to(locale_root_path) }
       Expected response to be a redirect to <http://www.example.com/en> but was a redirect to <http://www.example.com/?locale=en>.
       Expected "http://www.example.com/en" to be === "http://www.example.com/?locale=en".
     # ./spec/requests/authentication_pages_spec.rb:106:in `block (6 levels) in <top (required)>'

我找到了一些此错误的示例,但没有一个与I18网址格式相关。

任何帮助将不胜感激。我会一直在寻找。

1 个答案:

答案 0 :(得分:0)

我遇到了类似的问题,我已按照Devise wiki的建议设法解决了这个问题。我认为这个问题与Devise调用default_url_options的方式有关,但我不确定。

  

设置您的区域设置&amp;将default_url_options设为ApplicationController中的类方法

def set_locale
  I18n.locale = params[:locale]
end

def self.default_url_options(options={})
  options.merge({ :locale => I18n.locale })
end