Skip to content

Commit

Permalink
Modify test to check for deep equality in JSON
Browse files Browse the repository at this point in the history
We were previously checking if two JSON as strings were equal but the order of the JSON fields is not guaranteed. Pulling this change out of the Play 2.9 upgrade to simplify the PR: #26783. While on Play 2.8 the test is still passing but in Play 2.9 starts failing. The order of the fields in the string is not guardanteed though, so we're now checking the actual JSON.
  • Loading branch information
ioannakok committed Jan 22, 2024
1 parent f9b5c69 commit 33de6cf
Showing 1 changed file with 43 additions and 12 deletions.
55 changes: 43 additions & 12 deletions article/test/LiveBlogControllerTest.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package test

import agents.DeeplyReadAgent
import controllers.LiveBlogController
import org.mockito.Mockito._
import org.mockito.Matchers.{anyObject, anyString}
Expand All @@ -11,19 +10,19 @@ import play.api.test.Helpers._
import org.scalatest.{BeforeAndAfterAll, DoNotDiscover}
import org.scalatestplus.mockito.MockitoSugar
import model.{
EmailField,
FieldType,
LiveBlogPage,
Topic,
TopicResult,
TopicType,
MessageUsData,
FieldType,
EmailField,
MessageUsConfigData,
NameField,
TextAreaField,
Topic,
TopicResult,
TopicType,
}
import play.api.libs.json.{JsValue, Json}
import topics.TopicService
import services.{NewsletterService, MessageUsService}
import services.{MessageUsService, NewsletterService}
import services.newsletters.{NewsletterApi, NewsletterSignupAgent}

@DoNotDiscover class LiveBlogControllerTest
Expand Down Expand Up @@ -331,10 +330,42 @@ import services.newsletters.{NewsletterApi, NewsletterSignupAgent}
)(fakeRequest)
status(result) should be(200)

val content = contentAsString(result)
content should include(
"messageUs\":{\"formId\":\"mock-form-id\",\"formFields\":[{\"_type\":\"model.NameField\",\"id\":\"nameField1\",\"label\":\"name\",\"name\":\"name\",\"required\":true,\"type\":\"text\"},{\"_type\":\"model.EmailField\",\"id\":\"emailField1\",\"label\":\"email\",\"name\":\"email\",\"required\":true,\"type\":\"email\"},{\"_type\":\"model.TextAreaField\",\"id\":\"textAreaField1\",\"label\":\"textArea\",\"name\":\"textArea\",\"required\":true,\"type\":\"textarea\",\"minlength\":0,\"maxlength\":1000}]}",
)
val actualMessageUsContent: JsValue = contentAsJson(result).result.get("messageUs")
val expectedMessageUsContent: String =
"""
|{
| "formId":"mock-form-id",
| "formFields": [
| {
| "_type":"model.NameField",
| "name":"name",
| "label":"name",
| "id":"nameField1",
| "type":"text",
| "required":true
| },
| {
| "_type":"model.EmailField",
| "name":"email",
| "label":"email",
| "id":"emailField1",
| "type":"email",
| "required":true
| },
| {
| "name":"textArea",
| "label":"textArea",
| "id":"textAreaField1",
| "maxlength":1000,
| "minlength":0,
| "_type":"model.TextAreaField",
| "type":"textarea",
| "required":true
| }]
|}
|""".stripMargin

assert(actualMessageUsContent.equals(Json.parse(expectedMessageUsContent)))
}

it should "return no message us data, if not available" in new Setup {
Expand Down

0 comments on commit 33de6cf

Please sign in to comment.