简体   繁体   English

Rails,minitest:如何测试控制器的输出

[英]Rails, minitest: How to test the output of a controller

My controller looks like this: 我的控制器如下所示:

class CalculatorController < ApplicationController

  def calculate
    money = params[:money].to_f
   @worktime =  divide(money, 25)
  end

  private

  def divide(money, worktime)
    output = money/worktime
  end
end

I wrote a Test like this: 我写了这样的测试:

require 'test_helper'

class CalculatorControllerTest < ActionDispatch::IntegrationTest
  test "response" do
    get calculate_path
    assert_equal 200, response.status
  end
end

This test passes. 该测试通过。 Now, I want to write a test, to check, whether the output is correct. 现在,我想编写一个测试,以检查输出是否正确。 I tried this: 我尝试了这个:

require 'test_helper'

class CalculatorControllerTest < ActionDispatch::IntegrationTest
  test "response" do
    get calculate_path(money: 25)
    assert_equal 200, response.status
    assert_equal 1, @response.worktime
  end
end

But this error occurs: NoMethodError: undefined method worktime'` 但是会发生此错误: NoMethodError: undefined method

How do I test the output of the controller? 如何测试控制器的输出? ( In my case @worktime) (以我为例,@ worktime)

There's a good writeup here on the changes to testing in Rails 5: http://blog.bigbinary.com/2016/04/19/changes-to-test-controllers-in-rails-5.html 关于Rails 5中的测试更改,这里有一篇很好的文章: http : //blog.bigbinary.com/2016/04/19/changes-to-test-controllers-in-rails-5.html

In Rails 4, you would use "assigns" to check that: 在Rails 4中,您将使用“ assigns”来检查:

assert_equal 1, assigns(:worktime)

You can still have this functionality by including the rails-controller-testing gem https://github.com/rails/rails-controller-testing in your Gemfile in the test group. 您仍然可以通过在测试组的Gemfile中包含rails-controller-testing gem https://github.com/rails/rails-controller-testing来具有此功能。 More here from dhh: dhh的更多信息:

https://github.com/rails/rails/issues/18950 https://github.com/rails/rails/issues/18950

In Ruby @variables are instance variables. 在Ruby中, @variables variables是实例变量。 Not globals. 不是全局变量。 Instance variables are always private*. 实例变量始终是私有*。

So @worktime is an instance variable that belongs to CalculatorController - not CalculatorControllerTest . 因此, @worktime是属于CalculatorController的实例变量,而不是CalculatorControllerTest

Ruby lets you access an instance variable without declaring it. Ruby使您无需声明即可访问实例变量。

So this example: 所以这个例子:

require 'test_helper'

class CalculatorControllerTest < ActionDispatch::IntegrationTest
  test "response" do
    get calculate_path(money: 25)
    assert_equal 200, response.status
    assert_equal 1, @response.worktime
  end
end

Is actually in practice: 实际上是在实践中:

require 'test_helper'

class CalculatorControllerTest < ActionDispatch::IntegrationTest
  test "response" do
    get calculate_path(money: 25)
    assert_equal 200, response.status
    assert_equal 1, nil.worktime # since @worktime is not declared in this scope.
  end
end 

Previously you could pry into the inner workings of the controller with assigns which was removed in Rails 5. You can restore it with a gem but for new applications you should use a different approach to testing - you should instead test your controllers by testing the actual output in terms of response codes and JSON or HTML output. 以前,你可以窥探控制器与内部运作assigns这是Rails中取出5.您可以将其恢复用的宝石 ,但对于新的应用程序,你应该使用不同的测试方法-你应该改为通过测试实际测试你的控制器以响应代码和JSON或HTML输出的形式输出。 Not how it does its job. 不是它的工作方式。

require 'test_helper'

class CalculatorControllerTest < ActionDispatch::IntegrationTest
  test "response" do
    get calculate_path(money: 25)
    assert_equal 200, response.status
    assert_select "#result", "1"
  end
end  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM