Skip to content

Commit

Permalink
Use mockery::stub to mock tests #254
Browse files Browse the repository at this point in the history
  • Loading branch information
andrie committed Oct 18, 2017
1 parent dab8811 commit f714257
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 41 deletions.
45 changes: 33 additions & 12 deletions tests/testthat/test-0-is-404.R
@@ -1,31 +1,52 @@
if(interactive()) library(testthat)
library(mockery)

context("is.404")

test_that("is.404", {
skip_if_offline()
expect_true(is.404("http://mran.microsoft.com/snapshot/1972-01-01", warn = FALSE))
expect_false(is.404("http://mran.microsoft.com/snapshot"))
expect_false(is.404("http://mran.microsoft.com/snapshot/2015-05-01"))
expect_true(
is.404("http://mran.microsoft.com/snapshot/1972-01-01", warn = FALSE)
)
expect_false(
is.404("http://mran.microsoft.com/snapshot")
)
expect_false(
is.404("http://mran.microsoft.com/snapshot/2015-05-01")
)
})

test_that("is.404 works with https", {

if(!httpsSupported()) skip("https not supported")
expect_true(suppressWarnings(is.404("https://mran.microsoft.com/snapshot/1972-01-01")))
expect_false(is.404("https://mran.microsoft.com/snapshot"))
expect_false(is.404("https://mran.microsoft.com/snapshot/2015-05-01"))
expect_true(
suppressWarnings(is.404("https://mran.microsoft.com/snapshot/1972-01-01"))
)
expect_false(
is.404("https://mran.microsoft.com/snapshot")
)
expect_false(
is.404("https://mran.microsoft.com/snapshot/2015-05-01")
)

})

test_that("is.404 gracefully deals with https URLs when https not supported", {

with_mock(`checkpoint:::httpsSupported` = function(mran) FALSE, {
# if(!httpsSupported()) skip("https not supported")
expect_true(is.404("https://mran.microsoft.com/snapshot/1972-01-01", warn = FALSE))
expect_true(is.404("https://mran.microsoft.com/snapshot", warn = FALSE))
expect_true(is.404("https://mran.microsoft.com/snapshot/2015-05-01", warn = FALSE))
})
stub(is.404, "httpsSupported", function(mran) FALSE)
expect_true(
is.404("https://mran.microsoft.com/snapshot/1972-01-01", warn = FALSE)
)

# stub(is.404, "httpsSupported", function(mran) FALSE)
expect_true(
is.404("https://mran.microsoft.com/snapshot", warn = FALSE)
)

# stub(is.404, "httpsSupported", function(mran) FALSE)
expect_true(
is.404("https://mran.microsoft.com/snapshot/2015-05-01", warn = FALSE)
)

})

Expand Down
22 changes: 12 additions & 10 deletions tests/testthat/test-2-snapshot-dates.R
@@ -1,4 +1,5 @@
if(interactive()) library("testthat")
library(mockery)
context("getValidSnapshots finds valid dates")


Expand Down Expand Up @@ -26,16 +27,17 @@ test_that("suggests a reasonable alternative", {
})

test_that("works with no network connection", {
with_mock(`getValidSnapshots` = function(...){x <- character(0); class(x) = "error"; x}, {
expect_message(stopIfInvalidDate("2015-06-05"),
"Unable to connect to MRAN. Skipping some date validations."
)
with_mock(`getValidSnapshots` = function(...){x <- character(0); class(x) = "error"; x}, {
expect_message(stopIfInvalidDate("2015-06-05"),
"Unable to connect to MRAN. Skipping some date validations."
)
})
})
stub(stopIfInvalidDate, "getValidSnapshots",
function(...){x <- character(0); class(x) = "error"; x}
)
expect_message(
stopIfInvalidDate("2015-06-05"),
"Unable to connect to MRAN. Skipping some date validations."
)
expect_message(
stopIfInvalidDate("2015-06-05"),
"Unable to connect to MRAN. Skipping some date validations."
)
})

test_that("works on local file", {
Expand Down
26 changes: 18 additions & 8 deletions tests/testthat/test-7-anyRfiles.R
Expand Up @@ -24,15 +24,25 @@ test_that("finds no R files in fake checkpoint archive", {
})

test_that("deals correctly with invalid project paths", {
with_mock(
`base::normalizePath` = function(x, winslash, mustWork)"~/",
`base::readline` = function(x)"y",
expect_null(validateProjectFolder(td))
stub(validateProjectFolder,
"normalizePath",
function(x, winslash, mustWork)"~/"
)
stub(validateProjectFolder,
"readline",
function(x)"y"
)
expect_null(validateProjectFolder(td))

with_mock(
`base::normalizePath` = function(x, winslash, mustWork)"~/",
`base::readline` = function(...)"n",
expect_error(validateProjectFolder(td), "Scanning stopped.")
stub(validateProjectFolder,
"normalizePath",
function(x, winslash, mustWork)"~/"
)
stub(validateProjectFolder,
"readline",
function(x)"n"
)

expect_error(validateProjectFolder(td), "Scanning stopped.")

})
25 changes: 14 additions & 11 deletions tests/testthat/test-8-authoriseFileSystemUse.R
@@ -1,26 +1,29 @@
# tests for checking file system authorization
if(interactive()) library(testthat)
library(mockery)

context("file system authorization")
td <- file.path(tempdir(), "checkpoint_not_auth")
unlink(td, recursive = TRUE)

test_that("stops without authorization in interactive mode", {
with_mock(
`base::readline` = function(prompt)"n",
expect_error(
authorizeFileSystemUse(td, interactive=TRUE),
"Cannot proceed without access to checkpoint directory"
)
stub(authorizeFileSystemUse,
"readline",
function(prompt)"n"
)
expect_error(
authorizeFileSystemUse(td, interactive=TRUE),
"Cannot proceed without access to checkpoint directory"
)
})

test_that("continues with authorization in interactive mode", {
with_mock(
`base::readline` = function(prompt)"y",
expect_null(
authorizeFileSystemUse(td, interactive=TRUE)
)
stub(authorizeFileSystemUse,
"readline",
function(prompt)"y"
)
expect_null(
authorizeFileSystemUse(td, interactive=TRUE)
)
})

Expand Down

0 comments on commit f714257

Please sign in to comment.