API를 통해 웹 서비스에 액세스하기위한 라이브러리를 작성하고 있습니다. 나는 API 액션부작용 모나드의 테스트 법칙
case class ApiAction[A](run: Credentials => Either[Error, A])
을 나타내는 간단한 클래스를 정의한 웹 서비스를 수행 일부 기능이 나는 또한
implicit val monad = new Monad[ApiAction] { ... }
그래서 내가 할 수있는 ApiAction
모나드했습니다
// Retrieve foo by id
def get(id: Long): ApiAction[Foo] = ???
// List all foo's
def list: ApiAction[Seq[Foo]] = ???
// Create a new foo
def create(name: String): ApiAction[Foo] = ???
// Update foo
def update(updated: Foo): ApiAction[Foo] = ???
// Delete foo
def delete(id: Long): ApiAction[Unit] = ???
를 호출 do like something
create("My foo").run(c)
get(42).map(changeFooSomehow).flatMap(update).run(c)
get(42).map(_.id).flatMap(delete).run(c)
는 지금은 monadLaw.rightIdentity
이 equal
def rightIdentity[A](a: F[A])(implicit FA: Equal[F[A]]): Boolean =
FA.equal(bind(a)(point(_: A)), a)
를 사용하고 더 Equal[ApiAction]
이없는
val x = 42
val unitX: ApiAction[Int] = Monad[ApiAction].point(x)
"ApiAction" should "satisfy identity law" in {
Monad[ApiAction].monadLaw.rightIdentity(unitX) should be (true)
}
때문에 문제가 모나드 법을 테스트합니다.
[error] could not find implicit value for parameter FA: scalaz.Equal[ApiAction[Int]]
[error] Monad[ApiAction].monadLaw.rightIdentity(unitX) should be (true)
[error] ^
문제는 내가 어떻게 Equal[ApiAction]
을 정의 할 수 있는지 상상할 수 없다는 것입니다. ApiAction
은 필수 기능이며 기능에 대한 동등한 관계를 알지 못합니다. 물론 ApiAction
'을 실행 한 결과를 비교하는 것도 가능하지만 동일하지는 않습니다.
뭔가 잘못하고있는 것처럼 느껴지거나 필수적인 것을 이해하지 못합니다. 따라서 내 질문은 다음과 같습니다.
ApiAction
이 모나드가되는 것이 맞습니까?ApiAction
을 올바르게 디자인 했습니까?- 모나드 법을 어떻게 테스트해야합니까?
무한 도메인이있는 함수에 대해 계산 가능한 동등 관계가 없습니다. 당신은 봉인 된 형질의 추상 메소드를'run'으로 만들 수 있고,'ApiAction'을 파생 된 케이스 클래스와 케이스 객체로 구현할 수 있습니다. –