RSpec的护栏,水豚 - 不同的故障有:JS =>真实,不(RSpec-rails-capy

2019-08-01 22:27发布

我建立个人的计费设置屏幕。 控制器/视图是在管理的命名空间。

当不运行第一个测试:JS =>真的,我得到一个失败,我以为是下降到一个事实,即链接不为它的工作,它使用一个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时,我得到这似乎是从运行时,屏幕上的无效用户/密码闪动起来登录失败。

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选项?

Answer 1:

你可能有config.use_transactional_fixtures = true在你spec_helper.rb。 它不与水豚工作的JavaScript功能,因为服务器和单独的线程上运行的浏览器客户端。 由于这样的事实:在服务器上的数据库事务是不是客户端可见,客户有没有你在让!(),因此用户无法登录到系统创建的用户线索。

你需要关闭交易灯具和清理数据库前/每次运行后(考虑宝石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

上面的代码片断摘自联系人管理应用程序自述和稍微修改



文章来源: RSpec-rails-capybara - different failures with :js => true and without