与RSPEC之间非常相似的规格之间共享代码
在彼此非常相似的规格之间共享代码(例如:一个操作的请求规格,除了调用服务对象和服务对象本身的规格外,
我在应用程序中有几个操作没有做很多事情,而是要组织HTTP参数,调用服务对象,然后根据诉讼的结果和目标将结果返回为200、20或422状态代码。因此,这些操作的规格与服务对象的规格有些相似。
操作:
a)一个部分,我将使用LET(:Model)和Factorybot进行测试所需的模型预加载数据库。
b)我使用帖子,get等调用操作的部分传递适当的参数。
c)我使用期望来验证DE HTTP响应的部分。
D)我使用期望来验证预期数据库更改的部分。
服务对象:
a)一个部分,我将使用LET(:model)和Factorybot进行测试所需的模型预加载数据库。
b)我实例化(或使用主题)服务对象的会话并调用该方法执行通过适当的参数。
c)我使用期望来验证方法的响应的会话。
D)我使用期望来验证预期数据库更改的部分。
因此,您会注意到我的测试的大约一半是相同的。我可以通过对A节的共享上下文使用共享上下文来解决它)(尽管a)规范的A)和SERVICE SPEC的a)条之间会有少量差异。但是,这仍然是很多重复的代码。
我可以使用共享的示例,形状示例的问题是,它是一种付出或留下的情况,不是为了共享规格的各个部分而构建的,而是整个测试。
在类似规格之间共享代码的最佳方法是什么?
Share code between specs that are very similar to each other (ex: a request spec of an action that doesn't do anything other than calling a service object and the spec of the service object itself)
I have a few actions in my app that doesn't do a whole lot of stuff but to organize the HTTP params, call a service object, and return the result either as a 200, 201, or 422 status code depending on the outcome and the objective of the action. So the specs for the actions are somewhat similar to the specs of the service object.
Action:
a) A section where I preload my database with the models I need for my test with let(:model) and FactoryBot.
b) A section where I call the action using post, get, etc passing the appropriate parameters.
c) A section where I use the expectations to validate de HTTP response.
d) A section where I use expectations to validate stuff like expected database changes.
Service object:
a) A section where I preload my database with the models I need for my test with let(:model) and FactoryBot.
b) A session where I instantiate (or use the subject) the service object and call the method to execute it passing the appropriate parameters.
c) A session where I use expectations to validate the method's response.
d) A section where I use expectations to validate stuff like expected database changes.
So you will notice that about half of the sections of my tests are about the same. I could party solve it by using shared contexts for section a) (although often there will be slight differences between section a) of the request spec and section a) of the service spec). But still, that would be a lot of duplicated code.
I could use shared examples, the problem with shaped examples is that it's a take it or leave it situation, it's not built to share sections of a spec, but a whole test.
What's the best approach to share code between similar specs?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论