Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
309 views
in Technique[技术] by (71.8m points)

ruby on rails - Controller Specs vs Request Specs?

I'm working on a rails API and I'm now planning on writing some RSpec tests for the controllers. I've been reading around and I haven't been able to figure out what the actual difference between controller specs and request specs are and which one I should probably use if I'm testing an API.

question from:https://stackoverflow.com/questions/40851705/controller-specs-vs-request-specs

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Rails 3 & 4

Controller specs - A controller spec is an RSpec wrapper for a Rails functional test. It allows you to simulate a single http request in each example, and then specify expected outcomes

Request specs - Request specs provide a thin wrapper around Rails' integration tests, and are designed to drive behavior through the full stack, including routing (provided by Rails) and without stubbing (that's up to you).

So if you want to test API controllers I would recommend to use Controller specs as you are testing single requests.


Rails 5+

Rails 5 improved the speed and realism of request specs over Rails version 4's controller and request specs. The official recommendation of the Rails team and the RSpec core team is to write request specs instead (of controller specs).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...