第 5 章 控制器测试
5.1基础
rails generate rspec:controller home
# be_success 检查响应的状态是成 功(200 响应)还是失败(例如 500 异常)。
expect(response).to have_http_status "200"
be_success匹配器 ,have_http_status()匹配器
5.2要验证身份的controller test
如果程序有Devise。测试的时候,需要身份验证,则使用Devise Test helpers
Devise includes some test helpers for controller and integration tests. In order to use them, you need to include the repective module in you test cases/specs.
包含一个测试模块.
class PostsControllerTest < ActionController::TestCase include Devise::Test::ControllerHelpers end
class PostsTests < ActionDispatch::IntegrationTest include Devise::Test::IntegrationHelpers end
RSpec.configure do |config| config.include Devise::Test::IntegrationHelpers, type: :feature
config.include Devise::Test::ControllerHelpers, type: :controller
config.include Devise::Test::ControllerHelpers, type: :view
end
然后就可以使用sign_in, sign_out方法了
redirect_to 匹配器
以下测试都是get的测试。
5.3 测试用户输入。
HTTP GET外,还有POST, PATCH, DELETE与controller交互。
Method: FactoryBot::Syntax::Methods#attributes_for
#attributes_for(name, *traits_and_overrides, &block) ⇒ Hash
根据factories中的注册的name, 生成一个hash属性。
#expect(@user.projects.count).to eq(1) #替代方法
change()方法,by()方法,是哪里定义的?
在gem rspec-expectations 中定义的。
This class is part of a private API. 文档提示:私有类,尽量避免使用.
https://www.rubydoc.info/gems/rspec-expectations/RSpec/Matchers:change
#已经更新数据库,但内存里@project实例变量没有更新,所以要reload.
reload()方法 从数据库中根据接收者的primary-key id调取record并更新接收者的属性。
Reloading is commonly used in test suites to test something is actually written to the database, or when some action modifies the corresponding row in the database but not the object in memory:经常用于测试,测试记录实际已写入数据库,或者测试当一些action在数据库中修改了相关记录但在内存中已存在这个对象变量是否更新。
5.4 测试用户输入导致的错误
这里用到trait,在projects.rb的中定义的一个trait
trait :invalid do
name nil
end
⚠️ trait 和 factory继承的区别,一个是增加特点,用的时候需要和预构件一起用;一个是完全继承父类属性并增加自己的特色,是一个新的预构件。
5.5
处理非 HTML 输出 ,如何编写对JSON格式的测试。
bin/rails g rspec:controller tasks
spec/controllers/tasks_controller_spec.rb 测试控制器show动作
content_type方法属于 ActionDispatch::Response类的实例方法。
5.6小结
控制器测试,测试的是user登陆和未登陆状态下,已经不同用户权限下,对action 是否成功的测试。
在实际测试中,控制器测试已经不流行了。