在球拍中模拟依赖行为
Mocking dependency behaviour in racket
我刚开始在球拍中使用模拟,我想测试以下代码:
(define (tables #:connector [postgresql-connect postgresql-connect]
#:list-tables [list-tables list-tables])
; connect to the db
(define pgc
(postgresql-connect #:user "app_user"
#:database "test_database"
#:password "something_secure"))
; find all the tables
(list-tables pgc))
所以我有几个测试用例:
(module+ test
(require rackunit)
(require mock)
(require mock/rackunit)
(test-case "it returns a report structure"
(check-eq? (sniffer "not://a.url/test") "not://a.url/test"))
(test-case "it calls the db connector with the right arguments"
(define connector-mock (mock #:behavior postgresql-connect))
(sniffer "not://a.url/test" #:connector connector-mock)
(check-mock-called-with? connector-mock (arguments #:database "test_database"
#:password "something_secure"
#:user "app_user")))
(test-case "it compiles a list of tables to examine"
(define connector-mock (mock #:behavior postgresql-connect ))
(define list-tables-mock (mock #:behavior list-tables ))
(sniffer "not://a.url/test" #:connector connector-mock #:list-tables list-tables-mock)
(check-mock-called-with? list-tables-mock (arguments connector-mock))))
当我 运行 我得到的测试:
--------------------
it calls the db connector with the right arguments
; ERROR
tcp-connect: connection failed
hostname: localhost
port number: 5432
system error: Connection refused; errno=61
--------------------
--------------------
it compiles a list of tables to examine
; ERROR
tcp-connect: connection failed
hostname: localhost
port number: 5432
system error: Connection refused; errno=61
--------------------
这让我想到了这个。我如何让模拟模仿 postgresql-connect
而无需调用实际实现?
当您使用 #:behavior
关键字时,您是在说 mock 实际上应该调用该函数。您正在传递 postgresql-connect
,因此模拟实际上会尝试连接。您将需要传递一个不同的函数,该函数实际上并不进行连接但采用相同的参数,可能使用 mock
库中的 define-opaque
。
mock
文档中的 This example 可能会有帮助。
我刚开始在球拍中使用模拟,我想测试以下代码:
(define (tables #:connector [postgresql-connect postgresql-connect]
#:list-tables [list-tables list-tables])
; connect to the db
(define pgc
(postgresql-connect #:user "app_user"
#:database "test_database"
#:password "something_secure"))
; find all the tables
(list-tables pgc))
所以我有几个测试用例:
(module+ test
(require rackunit)
(require mock)
(require mock/rackunit)
(test-case "it returns a report structure"
(check-eq? (sniffer "not://a.url/test") "not://a.url/test"))
(test-case "it calls the db connector with the right arguments"
(define connector-mock (mock #:behavior postgresql-connect))
(sniffer "not://a.url/test" #:connector connector-mock)
(check-mock-called-with? connector-mock (arguments #:database "test_database"
#:password "something_secure"
#:user "app_user")))
(test-case "it compiles a list of tables to examine"
(define connector-mock (mock #:behavior postgresql-connect ))
(define list-tables-mock (mock #:behavior list-tables ))
(sniffer "not://a.url/test" #:connector connector-mock #:list-tables list-tables-mock)
(check-mock-called-with? list-tables-mock (arguments connector-mock))))
当我 运行 我得到的测试:
--------------------
it calls the db connector with the right arguments
; ERROR
tcp-connect: connection failed
hostname: localhost
port number: 5432
system error: Connection refused; errno=61
--------------------
--------------------
it compiles a list of tables to examine
; ERROR
tcp-connect: connection failed
hostname: localhost
port number: 5432
system error: Connection refused; errno=61
--------------------
这让我想到了这个。我如何让模拟模仿 postgresql-connect
而无需调用实际实现?
当您使用 #:behavior
关键字时,您是在说 mock 实际上应该调用该函数。您正在传递 postgresql-connect
,因此模拟实际上会尝试连接。您将需要传递一个不同的函数,该函数实际上并不进行连接但采用相同的参数,可能使用 mock
库中的 define-opaque
。
mock
文档中的 This example 可能会有帮助。