Stubbing controller actions in RSpec request specs

后端 未结 2 1289
时光说笑
时光说笑 2021-01-15 09:12

I am writing an API test case for server errors. I want to stub a controller action to raise an error to simulate the server error (500). In the requests spec, the con

相关标签:
2条回答
  • 2021-01-15 09:57

    It's actually an instance variable: @controller.

    0 讨论(0)
  • 2021-01-15 09:58

    I ended up stubbing the controller method using any_instance.

    it "Should return 500 upon server error" do
      UsersController.any_instance.stub(:index).and_raise(ArgumentError)
      get "/users.json"
      response.code.should eq("500")
      response.body.should have_json_path("error")
    end
    

    Note:

    Stubbing controller methods in a request spec doesn't make sense. But... In this case I am using the request spec suite as the acceptance criteria. One of the requirement was to ensure, all the error codes and messages match the API design.I was able to induce the server to raise all the HTTP error codes specified in the API design. Only edge case was the internal server error(i.e. 500). I had no means of inducing the controller to raise this error. Since, I am testing error reply and since this reply is independent of the location and source of the exception I decided to stub it.

    0 讨论(0)
提交回复
热议问题