0

I'm trying to test an api controller action with rspec, but since i am quite new to rspec i can't make it work. I have the following index action: render json: @website.jobs

I have to find all associated jobs to a website by name. Here is my spec:

let(:endpoint) { FactoryGirl.create(:website) }
let(:job) { FactoryGirl.create(:job) }

subject do
  get :index, params: { format: :json, 
                       name: endpoint.name }
end

expect(subject.body).to include(:job)

Am i doing something wrong, or am i missing something ?

0

1 Answer 1

1

You cannot check the object directly with the JSON response. You can do something like this:

it "response with JSON body containing expected jobs" do
  hash_body = JSON.parse(response.body)
  expect(hash_body).first.to match({
    id: job.id,
    title: job.title
  })
end

You need to change the attributes according to your Job model.

A good tutorial for testing JSON using Rspec: Pure RSpec JSON API testing

Hope this helps.

Sign up to request clarification or add additional context in comments.

3 Comments

Helped me if not OP! +1 for the tutorial
Not sure if i am explaining it, or understanding wrong. Don't i need to assign a job to the website first ? So i can make a request for that website and the output should be the assigned job. Then all that outputted to a json format.
So you can do that like this: FactoryGirl.create(:job, website: website) and you need to define the website attribute in the factory

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.