I'm in the process of learning to implement RSpec on API endpoints, and following this tutorial, https://labs.kollegorna.se/blog/2015/04/build-an-api-now/.
In the attached github (https://github.com/vasilakisfil/rails_tutorial_api/blob/008af67e88897a5bcde714ce13d39a26ec70fba7/spec/apis/users_spec.rb), some of the testing methods are:
But when I tried these in my own application, it resulted in errors.
method_missing': undefined method
it_returns_status' for RSpec::ExampleGroups::ApiV1ProjectsController::Show:Class (NoMethodError)
And also couldn't find these methods upon googling.
Has anyone used these before and know if they are part of an additional library? Or something else? I'm pretty new to RSpec.
To test status use the specific validator instead of it_returns_status
:
expect(response).to have_http_status(:success)
To test JSON answer use parser and test the field instead of attributes pair:
record = JSON.parse(response.body)
expect(record["root"]).to eq 'users'
expect(record["number"]).to eq 6
To know more info on JSON testing see the article.
If do you meant the resource is path use path validators, like:
expect(current_path).to eq(root_path(@user))