简体   繁体   English

Ruby on Rails:如何测试控制器?

[英]Ruby on Rails : how to test a controller?

I'm learning Ruby.我正在学习 Ruby。 I'm now trying to test one of my controller.我现在正在尝试测试我的控制器之一。

My test file is myapp/test/testing.rb while my controller is located at myapp/app/controllers/test_controller.rb .我的测试文件是myapp/test/testing.rb而我的控制器位于myapp/app/controllers/test_controller.rb

The content of testing.rb is testing.rb的内容是

data = testController.mymethod()
puts(data)

But when doing但是在做的时候

ruby myapp/test/testing.rb

in the terminal, I get a warning :在终端中,我收到警告:

Traceback (most recent call last):myapp/test/testing.rb:6:in `': uninitialized constant testController (NameError)回溯(最近一次调用最后一次):myapp/test/testing.rb:6:in `': 未初始化的常量 testController (NameError)

Could someone explain me what i'm doing is wrong and how I should do this ?有人可以向我解释我在做什么是错误的,我应该怎么做?

Thanks !谢谢 !

The currently accepted way to test rails controllers is by sending http requests to your application and writing assertions about the response.当前接受的测试 Rails 控制器的方法是向您的应用程序发送 http 请求并编写有关响应的断言。

Rails has ActionDispatch::IntegrationTest which provides integration tests for Minitest which is the Ruby standard library testing framework. Rails 有ActionDispatch::IntegrationTest ,它为 Minitest 提供集成测试,Minitest 是 Ruby 标准库测试框架。 Using the older approach of mocking out the whole framework with ActionDispatch::ControllerTest is no longer recommended for new applications.对于新应用程序,不再推荐使用旧的方法通过ActionDispatch::ControllerTest整个框架。

This is your basic "Hello World" example of controller testing:这是控制器测试的基本“Hello World”示例:

require 'test_helper'

class BlogFlowTest < ActionDispatch::IntegrationTest
  test "can see the welcome page" do
    get "/"
    assert_select "h1", "Welcome#index"
  end
end

You can also use RSpec which is different test framework with a large following.您还可以使用 RSpec,它是一个具有大量追随者的不同测试框架。 In RSpec you write request specs which are just a thin wrapper on top of ActionDispatch::IntegrationTest.在 RSpec 中,您编写请求规范,它只是 ActionDispatch::IntegrationTest 之上的一个薄包装器。

RSpec is DSL written on Ruby special for this purpose. RSpec 是用 Ruby 编写的 DSL,专门用于此目的。 You have to be familiar with it to write tests of your code.您必须熟悉它才能编写代码测试。 As for RSpec team note: `至于 RSpec 团队注意:`

The official recommendation of the Rails team and the RSpec core team is to write request specs instead. Rails 团队和 RSpec 核心团队的官方建议是改为编写请求规范。 Request specs allow you to focus on a single controller action, but unlike controller tests involve the router, the middleware stack, and both rack requests and responses.请求规范允许您专注于单个控制器操作,但与控制器测试不同的是,测试涉及路由器、中间件堆栈以及机架请求和响应。 This adds realism to the test that you are writing, and helps avoid many of the issues that are common in controller specs.这增加了您正在编写的测试的真实性,并有助于避免控制器规范中常见的许多问题。 In Rails 5, request specs are significantly faster than either request or controller specs were in rails 4.在 Rails 5 中,请求规范比 rails 4 中的请求或控制器规范要快得多。

So you have to write request specs for your controller.所以你必须为你的控制器编写request规范。 Something like this: spec/controllers/users_controller_spec.rb像这样: spec/controllers/users_controller_spec.rb

RSpec.describe "Test", type: :request do
  describe "request list of all tests" do
    user = User.create(email: 'test@user.com', name: 'Test User')
    get users_path
    expect(response).to be_successful
    expect(response.body).to include("Test User")
  end
end

Something like this.像这样的东西。 Hope it will help希望它会有所帮助

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

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