2

I am newbie to scala

My sbt version is : 0.13.17

My Scala version is : Scala code runner version 2.12.6 -- Copyright 2002-2018, LAMP/EPFL and Lightbend, Inc.

I am trying to run the test case with the following code

class TestSpec extends WordSpec with Matchers with MockFactory with OneAppPerSuite {

  "it" should {
    "add 2 numbers" in new Testing() {
      val a = 2
      val b = 3
      val expected = 5
      val result: Int = add(2, 3)

      result shouldEqual expected
    }
  }

  trait Testing {

    def add(a: Int, b: Int): Int = {
      a + b
    }
  }
}

And i get the following error

discarded non-Unit value
 "add 2 numbers" in new Testing() {

In my build.sbt file i see this

"-Ywarn-value-discard", // Warn when non-Unit expression results are unused

I cant remove the above line and how do i make sure my test case executes.

I have gone through this link Suppress "discarded non-Unit value" warning. But not sure what needs to be done. Any help is highly appreciated

Avinash Reddy
  • 2,204
  • 3
  • 25
  • 44

3 Answers3

1
I just tried something like ... And it works.

class Testclass extends  FlatSpec with Matchers  with TestTrait {
  it should "add two integers" in  {
      val a: Int =  2
      val b: Int =  3
      val result = add(a, b)
      result shouldEqual(5)
    }
}

trait TestTrait {
  def add(a: Int, b: Int):Int = {
     a + b
  }
}
Praveen
  • 21
  • 3
-1

This warning arises because the assigned values a and b are not used. Maybe you meant to write:

val result: Int = add(a, b)
Serge Kireev
  • 176
  • 1
  • 8
-1

disable -Ywarn-value-discard in scalacOptions

Abhishek Sengupta
  • 2,938
  • 1
  • 28
  • 35