2014-06-10 28 views
0

在我的Rails應用4我指定我的語言環境如下: 配置/區域設置/查看/ show_user/en.yml 的內容:RSpec的不承認我的國際化標籤

en: 
    activerecord: 
    attributes: 
     user: 
     first_name: First name 
     last_name: Last name 
     ... 
    layouts: 
    header: 
     home: "Home" 
     help: "Help" 
     ... 
    footer: 
     about: "About" 
     contact: "Contact" 
    users: 
    new: 
     signup: "Sign up!" 
     create_my_account: "Create my account" 

當我跑我的應用程序在我的本地主機,我的所有標籤顯示正確。然而,我的RSpec的測試,

describe "with valid information" do 
    before do 
    fill_in "First name", with: "Jenny" 
    fill_in "Last name", with: "Johnson" 

當我做如下改變我的這部分失敗時失敗,/views/users/_fields.html.erb

<%= f.label t :first_name, scope: [:activerecord, :attributes, :user] %> 
<% # f.label :first_name %> 
<%= f.text_field :first_name %> 
<%= f.label :last_name %> 
<%= f.text_field :last_name %> 

我的RSpec的測試顯示:

1) User pages signup with valid information should create a user 
    Failure/Error: fill_in "First name", with: "Jenny" 
    Capybara::ElementNotFound: 
    Unable to find field "First name" 
    # ./spec/requests/user_pages_spec.rb:88:in `block (4 levels) in <top (required)>' 

我注意到我的窗體生成的html顯示first_name的不同ID,但我不知道這是否重要:

<label for="user_First name">First name</label> 
<input id="user_first_name" name="user[first_name]" type="text" /> 
<label for="user_last_name">Last name</label> 
<input id="user_last_name" name="user[last_name]" type="text" /> 

我的問題是:爲什麼水豚不能找到我的標籤,:first_name,而我的頁面在我的瀏覽器中正確顯示?

回答

0

這很重要。水豚用在一個多種方式定位參數fill_in

  • ,如果有一個文本字段(或類似的輸入)與ID,然後它會使用該字段
  • 如果有包含文本標籤,那麼它將使用該標籤的字段。

您正處於第二種情況,因此水豚使用標籤上的for屬性來查找文本字段。這不起作用,因爲標籤的標籤爲user_First name,但文本字段上的標識爲user_first_name

默認情況下,rails會從您作爲第一個參數傳遞的值中導出標籤文本和for屬性,所以您應該將屬性名稱作爲第一個參數,然後將人類可讀的翻譯作爲第二個參數

f.label :first_name, t(:first_name, scope: [:activerecord, :attributes, :user]) 

我對該文檔的閱讀是,尋找這個事務實際上是默認的 - 您可能根本不需要指定它。