1

I have an RSpec test currently written on an application that 100% works, it current returns exactly what I want.

The application call method returns a Json list and the test I have is then supposed to check if certain elements are equal to an equivalent Json passed in. Simple right?

describe App do
   subject(:jsonlist) do
   final_json = App.call(json)
   JSON.load(final_json)
end

it "checks the json" do
  expect(jsonlist[2]["num"].to eq "10.00")
end 

It works fine for one test. But after that, Rspec calls the application again and again, adding the data on top of the existing json elements (10 becomes 20, 30, 160 whatever).

Anyone got any ideas why this is happening and how I can get Rspec to stop calling the application when I only need it to call the returned data once?

Many thanks in advance. My apologies for any confusion, I'm still new here

Danzai
  • 21
  • 5
  • The indentation here looks mangled. Is that all inside `describe` or not? – tadman Feb 05 '21 at 00:51
  • The `subject` declaration might be the issue here as those are [a little weird](https://stackoverflow.com/questions/38437162/whats-the-difference-between-rspecs-subject-and-let-when-should-they-be-used). – tadman Feb 05 '21 at 00:52
  • @tadman Apologies, yes ```describe App``` is inside an ```RSpec.describe``` at the top of the test. Though this section appears to be where the issue is originating – Danzai Feb 05 '21 at 01:03
  • Does `App` preserve state somehow? It's hard to tell from this limited example. – tadman Feb 05 '21 at 01:28
  • @tadman ```App``` just returns an array of hashes converted into JSON from data passed in via the argument so doesn't maintain anything. It appears Rspec is memorising the returned output somehow when running the subject tests – Danzai Feb 05 '21 at 01:46
  • Just noticed that `expect(jsonlist[2]["num"].to eq "10.00")` should be `expect(jsonlist[2]["num"]).to eq "10.00"`. – tadman Feb 05 '21 at 01:48
  • You may want to compare the whole document, like `expect(jsonlist).to eq([ ... ])` with the expected content. – tadman Feb 05 '21 at 01:49
  • Can you fix the indentation? It's still not clear how this is structured. The `subject ... do` block implies there's additional content. – tadman Feb 05 '21 at 01:49
  • @tadman thanks heaps! I think that was just a typo. I agree with you in that I think ```subject``` is to blame here – Danzai Feb 05 '21 at 01:50
  • A) Try `let`. B) Fix synax! – tadman Feb 05 '21 at 03:16
  • I'm wondering if there are more than one test checking fields in the JSON? If so, each test would call subject all over again. So, if you have 10 tests, the app would be called 10 times. Posting more of the spec file would help me help you. – aridlehoover Feb 19 '21 at 09:48

0 Answers0