Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使用rspec&;测试节点数据库ajaxrails表单计算器;有限公司_Ruby On Rails_Ruby_Ajax_Rspec_Capybara - Fatal编程技术网

Ruby on rails 使用rspec&;测试节点数据库ajaxrails表单计算器;有限公司

Ruby on rails 使用rspec&;测试节点数据库ajaxrails表单计算器;有限公司,ruby-on-rails,ruby,ajax,rspec,capybara,Ruby On Rails,Ruby,Ajax,Rspec,Capybara,我有ajax rails数据库表单计算器。它运行良好,但我想正确地测试它。我用rspec和水豚进行了测试。当它是简单的ruby/rails实现时,所有测试都通过了。当我添加ajax时,所有测试都会失败。 我应该使用什么技术来测试ajax 感兴趣的计算器控制器: class InterestCalculatorController < ApplicationController def index @result_0 = 0 end def new resp

我有ajax rails数据库表单计算器。它运行良好,但我想正确地测试它。我用rspec和水豚进行了测试。当它是简单的ruby/rails实现时,所有测试都通过了。当我添加ajax时,所有测试都会失败。 我应该使用什么技术来测试ajax

感兴趣的计算器控制器:

class InterestCalculatorController < ApplicationController

  def index
    @result_0 = 0
  end

  def new

    respond_to do |format|
      format.html { render 'index.html.erb' }
      format.js
    end 

    # If accepted parameter is integer, then it shows in view as 5, when it
    # is float, it shows as 5.1  
    @first_0   = params[:a_0].to_f % 1 != 0 ? params[:a_0].to_f : params[:a_0].to_i
    @second_0  = params[:b_0].to_f % 1 != 0 ? params[:b_0].to_f : params[:b_0].to_i

    # How many percent is number from the number 
    number_to_number(@first_0, @second_0) 

  end

 private 

   def number_to_number(a = 0, b = 0)   
    # If the first number is zero, it sends 0% answer. If the second number is zero 
    # and the first number is nonzero, it sends infinity. Otherwise simple formula calculation.
    if a.zero?
      @result_0 = 0
    elsif b.zero?
      @result_0 = "infinity"
    else
      @result_0 = a.to_f / b.to_f * 100
    end 
  end
end
测试输出失败:

  5) interest calculator test persent at number to number
     Failure/Error: expect(page).to have_css("#answer_0", text: "100.0%")
       expected to find css "#answer_0" with text "100.0%" but there were no matches
     # ./spec/features/interest_calculator_number_to_number_spec.rb:38:in `block (2 levels) in <top (required)>'
5)逐数兴趣计算器测试
失败/错误:期望(第页)有“css”(“答案0”,文本:“100.0%”)
预期会找到带有文本“100.0%”的css“#答案_0”,但没有匹配项
#/规格/功能/兴趣\u计算器\u编号\u至\u编号\u规格。rb:38:in‘block(2层)in’

举一个“失败”、测试、错误等的例子@ThomasWalpole done您正在使用哪个JS驱动程序?看见(仅供参考,拼写为“百分比”而非percent)@ThomasWalpole感谢您的拼写错误更正。我使用gem'rspec rails',“~>3.5”gem'capybara'gem'launchy'gem'selenium webdriver'@ThomasWalpole发行版与selenium一起使用-没有mozilla geckodriver它就无法工作。例如,如果我想在另一个工作站上测试我的应用程序,我必须安装这个geckodriver。我想在没有第三方的情况下实施我的测试。
document.getElementById("answer_0").innerHTML = "<%= @result_0 %>"
document.getElementById("first_0").innerHTML = "<%= @first_0 %>"
document.getElementById("second_0").innerHTML = "<%= @second_0 %>"
  scenario 'persent at number to number' do   
    visit interest_calculator_index_path
    expect(page).to have_content('Interest Calculator')
    fill_in "a_0", with: 5
    fill_in "b_0", with: 5
    click_button("number_to_number")
    expect(page).to have_css("#answer_0", text: "100.0%")
  end
  5) interest calculator test persent at number to number
     Failure/Error: expect(page).to have_css("#answer_0", text: "100.0%")
       expected to find css "#answer_0" with text "100.0%" but there were no matches
     # ./spec/features/interest_calculator_number_to_number_spec.rb:38:in `block (2 levels) in <top (required)>'