简体   繁体   English

RSpec:如何进行一个shared_example测试,以测试许多都更新不同属性的许多不同控制器PATCH请求?

[英]RSpec: How can I make one shared_example test that tests many different controller PATCH requests that all update different attributes?

I have various controllers and I have tests for each one that test their update action. 我有各种控制器,并且每个控制器都有测试其更新操作的测试。 The tests have all the exact same structure: it tests if it a patch request will update and change the object, or not. 测试具有完全相同的结构:它测试修补程序请求是否会更新和更改对象。 The only difference between these tests are which attribute the tests check to see for its change assertion. 这些测试之间的唯一区别是测试检查哪个属性以查看其更改断言。 These attributes are unique to the controllers. 这些属性是控制器唯一的。

class CarsController 

def update
   # update attribute
   ...
end

end

and

class DogsController 

def update
   # update attribute
   ...
end

end

My tests (2 of many more): 我的测试(还有2个测试):

CarsSpec 
describe "PATCH" do
  it "should update the car" do
    expect do 
      patch :update, id: object.id, data: {make: "honda"}
    end.to change {object.reload.make}
  end
end

DogSpec 
describe "PATCH" do
  it "should update the dog" do
    expect do 
      patch :update, id: object.id, data: {breed: "husky"}
    end.to change {object.reload.breed}
  end
end

As you can see, they're the exact same structure of tests so naturally, to be DRY I want to extract them into a shared_example to be DRY. 如您所见,它们自然是测试的完全相同的结构,要成为DRY,我想将它们提取到shared_example中以使其成为DRY。 The idea is to have many more of these controllers but only actual test and that these controllers just pass in which field to update. 想法是要有更多这样的控制器,但只有实际测试,并且这些控制器只是通过要更新的字段。 It would involve something like 它会涉及到类似

shared_example "update" do
  it "updates the object" do
    expect do 
      patch :update, id.object.id, data: { customField: "new value" }
    end.to change { object.reload.customField }
  end
end

Where customField could be either breed or make in this case and the test would know to update those fields so it is generic and can apply to many of these controllers. 在这种情况下,customField可能是繁殖的,也可能是makeField的,并且测试会知道要更新这些字段,因此它是通用的,可以应用于许多这些控制器。 How can I achieve that or something similar? 我该如何达到目标或类似目标?

shared_example 's block accepts arguments: shared_example的块接受参数:

shared_example "update" do |custom_field|
  it "updates the object (field: #{custom_field})" do
    expect do 
      patch :update, id.object.id, data: { custom_field => "new value" }
    end.to change { object.reload.public_send(custom_field) }
  end
end

and call it as 并称其为

include_examples 'update', :make
include_examples 'update', :breed

暂无
暂无

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

相关问题 Rails-Rspec:是否可以在shared_example中排除/删除测试用例? - Rails - Rspec: Is it possible to exclude / remove a test case in a shared_example? Rspec:如何将数组传递给先前定义的shared_example? - Rspec: How pass array to shared_example previously defined? 如何将RSpec Rails Controller测试放在另一个目录中? - How can I put RSpec Rails Controller tests in a different directory? RSpec围绕time_shared_example - Rspec surrounding shared_example with timecop 如何干掉同一 controller 中不同操作共享的 RSpec 测试 - How to DRY up RSpec tests shared by different actions in same controller 如何使用rspec测试不同的路由,但是使用相同的控制器方法? - How can I test with rspec different routes but to the same controller method? 如何使用RSpec在Rails中测试update_attributes函数? - How can I test update_attributes function in Rails with RSpec? 在rspec测试中发布到不同的控制器 - post to a different controller in an rspec test 如何在一个上下文中嵌套上下文(已经在另一个上下文中)以在RSpec测试的共享示例中创建一个共享示例? - How to nest context within a context (already in another context) to make a shared example within a shared example for an RSpec test? Rspec:一次测试中如何有多个控制器? 如何在测试中更改控制器? - Rspec: How can I have many controllers in one test? How can I change controllers in a test?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM