3

I thought this would be simple, but I can't find how to do it.

All I want to do is write a spec that passes if a json file is returned instead of an HTML file.

This is what my test looks like right now:

 require 'spec_helper' describe "sessions" do before do @program =FactoryGirl.create(:program) @user = FactoryGirl.create(:user) end describe "user" do it "is logged in" do post "/api/v1/login", user_login: {email: @user.email, password: @user.password } response.status.should be(201) # response.format.should be(:json) # Can I do something like this? end end end 
4
  • Why do you care? The format isn't really worth testing, in my opinion. Instead, you can check whether the response contains the necessary data; the actual format used to transport the data is irrelevant. Commented Oct 9, 2013 at 19:17
  • I thought it would be the simplest way to know that my iOS app was getting the format it needed instead of an HTTP error page or something. Commented Oct 9, 2013 at 19:20
  • I want to make sure that I am getting the JSON response instead of a web page as a response. Since both would return 201, can you recommend what you would test for? Commented Oct 9, 2013 at 19:26
  • FYI, JSON-schema is an interesting tool for taking this further. You can check if the JSON has the right data in the right structure. Commented Oct 9, 2013 at 19:33

1 Answer 1

6

I suggest You to check the actual content as meagar already noted. In my projects I have used this gem for easier matching: https://github.com/collectiveidea/json_spec

It provides handy matchers to easy match json content: be_json_eql include_json have_json_path have_json_type have_json_size

response.body.should include_json("some_field: 'it definitely has'") 
Sign up to request clarification or add additional context in comments.

1 Comment

Got MultiJson::LoadError: 795: unexpected token at 'success: 'true'' do i frogot something?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.