jeudi 29 octobre 2020

RSpec HTTP request tests always passing

So I am having some trouble getting some RSpec tests to FAIL. No matter what I try they are always passing, well, one of the tests works correctly and I verified it can fail if I modify the code it is testing.

I am trying to test the content of JSON responses made to an external API to make sure my controller is sorting these return JSON objects correctly.

Am I missing something here? Why cant I get these to fail?

RSpec.describe 'Posts', type: :request do
  describe 'ping' do
      it 'returns status 200' do # This text block works correctly.
        get "/api/ping"
        expect(response.content_type).to eq("application/json; charset=utf-8")
        expect(response).to have_http_status(200)
      end
  end

  describe 'get /api/posts' do # all tests below always pass, no matter what.
    it 'should return an error json if no tag is given' do
      get "/api/posts" 
      expect(response.content_type).to eq("application/json; charset=utf-8")
      expect(response.body).to eq("{\"error\":\"The tag parameter is required\"}")
    end

    it 'should return a json of posts' do
      get "/api/posts?tags=tech" do
        expect(body_as_json.keys).to match_array(["id", "authorid", "likes", "popularity", "reads", "tags"])
      end
    end

    it 'should sort the posts by id, in ascending order when no sort order is specified' do
      get "/api/posts?tags=tech" do
        expect(JSON.parse(response.body['posts'][0]['id'].value)).to_be(1)
        expect(JSON.parse(response.body['posts'][-1]['id'].value)).to_be(99)
      end
    end

    it 'should sort the posts by id, in descending order when a descending order is specified' do
      get "/api/posts?tags=tech&direction=desc" do
        expect(JSON.parse(response.body['posts'][0]['id'].value)).to_be(99)
        expect(JSON.parse(response.body['posts'][-1]['id'].value)).to_be(1)
      end
    end
end

Within the get block of the 'should return an error json if no tag is given' do block I even tried expect(4).to eq(5) and even THIS passed!

Help much appreciated here!

Aucun commentaire:

Enregistrer un commentaire