RSpec-rails-capybara – 不同的失败与:js => true和without

我正在建立个人帐单的设置屏幕.控制器/视图位于管理命名空间中.

当运行第一个测试时没有: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,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,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来创建链接,而capybara没有运行它,因为我不使用:js =>真正的选择?

任何人都可以看到我在使用以下操作时出错:js =>真正的选择?

解决方法

你的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,稍作修改

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


kindeditor4.x代码高亮功能默认使用的是prettify插件,prettify是Google提供的一款源代码语法高亮着色器,它提供一种简单的形式来着色HTML页面上的程序代码,实现方式如下: 首先在编辑器里面插入javascript代码: 确定后会在编辑器插入这样的代码: <pre
这一篇我将介绍如何让kindeditor4.x整合SyntaxHighlighter代码高亮,因为SyntaxHighlighter的应用非常广泛,所以将kindeditor默认的prettify替换为SyntaxHighlighter代码高亮插件 上一篇“让kindeditor显示高亮代码”中已经
js如何实现弹出form提交表单?(图文+视频)
js怎么获取复选框选中的值
js如何实现倒计时跳转页面
如何用js控制图片放大缩小
JS怎么获取当前时间戳
JS如何判断对象是否为数组
JS怎么获取图片当前宽高
JS对象如何转为json格式字符串
JS怎么获取图片原始宽高
怎么在click事件中调用多个js函数
js如何往数组中添加新元素
js如何拆分字符串
JS怎么对数组内元素进行求和
JS如何判断屏幕大小
js怎么解析json数据
js如何实时获取浏览器窗口大小
原生JS实现别踩白块小游戏(五)
原生JS实现别踩白块小游戏(一)