require_relative "../spec/spec_helper" RSpec.describe "The resembles json matcher" do include RSpec::ResemblesJsonMatchers before :all do puts <<-WARNING.strip_heredoc NOTE: some of these are expected to fail, they are meant to demonstrate the output generated by failing specs WARNING end describe "a basic json document" do let(:document) do { "@id": "/posts/2016/test1", "@type": "Post", "title": "Hello World!", "body": "lorem ipsum", "created_at": "2016-03-01T00:03:42", "published_at": "2016-03-10T15:35:00" } end specify do expect(document).to resemble_json( { "@id": "/posts/:year/:title", "@type": eq("Post"), "title": "Hello World!", "body": "lorem ipsum", "created_at": "2016-03-01T00:03:42", "published_at": "2016-03-10T15:35:00" } ) end context "with several attributes that failed to match" do specify do expect(document).to resemble_json( { "@id": "/posts/:year/:title", "@type": eq("PostCollection"), "title": 42.0, "body": "lorem ipsum", "created_at": "2016-03-01T00:03:42", "published_at": "2016-03-10T15:35:00" } ) end end context "when the matcher is missing a field that is present in the document" do specify do expect(document).to resemble_json( { "@id": "/posts/:year/:title", "@type": eq("Post"), "body": "lorem ipsum", "created_at": "2016-03-01T00:03:42", "published_at": "2016-03-10T15:35:00" } ) end end context "when the document is missing a field that is present in the matcher" do specify do expect(document).to resemble_json( { "@id": "/posts/:year/:title", "@type": eq("Post"), "body": "lorem ipsum", "created_at": "2016-03-01T00:03:42", "published_at": "2016-03-10T15:35:00" } ) end end end describe "complex nested json documents" do let(:document) do { "@id": "/posts", "@type": "PostCollection", "nextPage": "/posts?page=2", "members": [ { "@id": "/posts/2016/test1", "@type": "Post", "title": "Hello World!", "body": "lorem ipsum", "created_at": "2016-03-01T00:03:42", "published_at": "2016-03-10T15:35:00" } ] } end specify do expect(document).to resemble_json( { "@id": "/posts", "@type": eq("PostCollection"), "nextPage": "/posts?page=2", "members": [ { "@id": "/posts/:year/:title", "@type": eq("Post"), "title": "Hello World!", "body": "lorem ipsum", "created_at": "2016-03-01T00:03:42", "published_at": "2016-03-10T15:35:00" } ] } ) end end end