2

我正在使用 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 URL 的格式相关。

任何帮助将不胜感激。我会继续寻找。

4

1 回答 1

0

我有一个类似的问题,我通过遵循Devise wiki的建议设法解决了这个问题。我认为这个问题与 Devise 调用的方式有关,default_url_options但我不确定。

设置您的语言环境并将您的 default_url_options 设置为 ApplicationController 中的类方法

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

def self.default_url_options(options={})
  options.merge({ :locale => I18n.locale })
end
于 2014-12-09T21:51:15.083 回答