2012-10-25 55 views
9

我正在建立一個個人賬單設置屏幕。控制器/視圖位於Admin命名空間中。RSpec導軌水豚 - 不同的失敗與:js =>真,沒有

當沒有運行第一個測試時:js => true我得到一個失敗,我認爲這是因爲鏈接不起作用,它使用js腳本來構建一組嵌套字段(基於Railscasts單一表格,多個表格 - 嵌套屬性)。

Failures: 

    1) Patient Setup create patient bill heading - with extended details -with valid data 
    Failure/Error: fill_in "Extended Bill Heading", :with => 'Regular Registration' 
    Capybara::ElementNotFound: 
     cannot fill in, no text field, text area or password field with id, name, or label 'Extended Bill Heading' found 
    # (eval):2:in `fill_in' 
    # ./spec/requests/admin/patient_setup_pages_spec.rb:52:in `block (4 levels) in <top (required)>' 

Finished in 0.83047 seconds 
3 examples, 1 failure, 2 pending 

但是當我使用:JS = true時,我得到這似乎是從運行時,屏幕上的無效用戶/密碼閃動起來登錄失敗。

enter image description here

Failures: 

    1) Patient Setup create patient bill heading - with extended details -with valid data 
    Failure/Error: click_link 'System' 
    Capybara::ElementNotFound: 
     no link with title, id or text 'System' found 
    # (eval):2:in `click_link' 
    # ./spec/requests/admin/patient_setup_pages_spec.rb:22:in `block (2 levels) in <top (required)>' 

Finished in 6.33 seconds 
3 examples, 1 failure, 2 pending 

這裏是用來備份這一切了代碼。

spec/requests/admin/patient_setup_spec.rb 

require 'spec_helper' 

feature 'Patient Setup' do 

    let!(:ci_user) { FactoryGirl.create(:user, 
             name: "configuration engineer", 
             password: "password", 
             password_confirmation: "password" 
            ) } 
    let!(:admin_role) { FactoryGirl.create(:admin_role) } 
    let!(:assignment) { FactoryGirl.create(:assignment, 
             :role => admin_role, 
             :user => ci_user 
             ) } 

    before do 
    visit login_path 
    fill_in "Name", with: "configuration engineer" 
    fill_in "Password", with: "password" 
    click_button "Login" 
    save_and_open_page 
    click_link 'System' 
    click_link 'Patient Setup' 
    end 

    describe "create patient bill heading" do 
    before do 
     click_link 'New Bill Heading' 
     fill_in 'Bill heading', :with => 'Consultation' 
     fill_in 'Abbreviation', :with => "CON"  
    end 

    context "- no extended details" do 
     pending 

     scenario "- with valid data" do 
     pending 
     click_button 'Create Patient bill heading' 
     page.should have_content('Patient Bill Heading created.') 
     end 
    end 

    context "- with extended details", :js => true do #(without :js => true 1st error) 
     before do 
     # save_and_open_page 
     click_link "Extended Bill Heading" 
     # save_and_open_page   
     end 

     scenario "-with valid data" do 
     save_and_open_page 
     fill_in "Extended Bill Heading", :with => 'Regular Registration' 
     end 
    end 

    end 
end 

這是我的工廠設置。

spec/factories.rb 

FactoryGirl.define do 

    # Users, Roles 
    factory :user do 
    name  "Joesephine Bloggs" 
    password "testmenow" 
    password_confirmation "testmenow" 
    end 

    factory :admin, :class => User do 
    sequence(:name) { |n| "Administrator-#{n}" } 
    password "adminiam" 
    password_confirmation "adminiam" 
    after(:create) do |user| 
     FactoryGirl.create(:assignment, :role => FactoryGirl.create(:admin_role), :user => user) 
    end 
    end 

    factory :role do 
    description { "Clerical-#{rand(99)}" } 

    factory :admin_role do 
     description "Admin" 
    end 
    end 

    factory :assignment do 
    user 
    role 
    end 

    # Patients Module 

    factory :patient_bill_heading do 


     sequence(:bill_heading) { |n| "bill-heading-#{n}" } 
     sequence(:abbreviation) { |n| "abbreviation-#{n}" } 


     factory :delete_patient_bill_heading, :class => PatientBillHeading do 
     bill_heading :bill_heading 
     abbreviation :abbreviation 
     end 

    end 
end 

這裏是我的視圖中的鏈接,以調用生成嵌套屬性字段的助手。

<p> 
    <%= link_to_add_fields "Extended Bill Heading", f, :patient_extended_bill_headings %> 
</p> 

這裏是幫手。

helpers/application_helper.rb 

    def link_to_add_fields(name, f, association, options={}) 
    defaults = { 
     :partial_name => nil 
    } 
    options = defaults.merge(options) 

    new_object = f.object.send(association).klass.new 
    id = new_object.object_id 
    fields = f.fields_for(association, new_object, child_index: id) do |builder| 
     if options[:partial_name].nil? 
     render(association.to_s.singularize + "_fields", f: builder) 
     else 
     render(options[:partial_name], f: builder) 
     end 
    end 
    link_to("#{name} <i class='icon-plus icon-white'></i>".html_safe, 
      '#', 
      class: "btn btn-success add_fields", 
      data: {id: id, fields: fields.gsub("\n", "")} 
      ) 
    end 

我想提高我的RSpec的測試知識,因爲我已經成功地建立了這個工作在我的應用一小時試圖找出爲什麼我得到的測試失敗之後。所以在應用程序中它可以工作,但我想了解如何讓我的測試通過。

我的斷言是一個錯誤是由於使用js創建鏈接,並且由於我沒有使用:js => true選項,因此水豚沒有運行它?

任何人都可以看到我做錯了,當使用:js => true選項?

回答

23

您的spec_helper.rb中可能有config.use_transactional_fixtures = true。它不支持Capybara JavaScript規範,因爲服務器和瀏覽器客戶端運行在不同的線程上。由於服務器上的數據庫事務對客戶端不可見,因此客戶端不知道您在let!()中創建的用戶,因此用戶無法登錄系統。

您需要在每次運行之前/之後關閉事務夾具並清理數據庫(請參閱gem database_cleaner)以瞭解您的js規範。

RSpec.configure do |config| 
    config.use_transactional_fixtures = false 

    config.before(:suite) do 
    DatabaseCleaner.clean_with :truncation 
    end 

    config.before(:each) do 
    if example.metadata[:js] 
     DatabaseCleaner.strategy = :truncation 
    else 
     DatabaseCleaner.strategy = :transaction 
    end 
    DatabaseCleaner.start 
    end 

    config.after(:each) do 
    DatabaseCleaner.clean 
    end 
end 

上面的代碼片斷是從the contact manager app readme取出並略作修改

+0

謝謝魏,即解決了這個問題。現在我要解決的下一個問題是Cancan'load_and_authorize_resource'! – user1149642

+0

我覺得你的語法錯了,不應該是'config.before(:each)do | example |'? – Noz

+0

不,它的工作原理 - 我在項目中使用該語法。我沒有讀過太多的源代碼。但我的猜測是,這與https://github.com/rspec/rspec-core/blob/master/lib/rspec/core/example_group.rb#L440有關。你的語法可能也會起作用。 – Wei