Hello,
I have a function that applies to an S4 object which contains a slot called @analysis:
function calculation(myObject) {
tmp <- myObjects at analysis
result <- ...operations on analysis...
return result
}
I am writing a unit test for this function. So I was hoping to create a mock object but I can't figure out how to do it:
test_that("test calculation function", {
mockMyObject<- mock(?????) #I am not sure what to put here
r<-calculation(mockMyObject)
expect_true(r,0.83625)
})
How can I create a mock S4 object??
Thanks in advance,
Ramiro
Using a mock of an S4 class
6 messages · Ramiro Barrantes, David Winsemius, Jeff Newmiller +1 more
On Feb 1, 2017, at 11:46 AM, Ramiro Barrantes <ramiro at precisionbioassay.com> wrote:
Hello,
I have a function that applies to an S4 object which contains a slot called @analysis:
function calculation(myObject) {
tmp <- myObjects at analysis
result <- ...operations on analysis...
return result
}
I am writing a unit test for this function. So I was hoping to create a mock object but I can't figure out how to do it:
test_that("test calculation function", {
mockMyObject<- mock(?????) #I am not sure what to put here
r<-calculation(mockMyObject)
expect_true(r,0.83625)
})
How can I create a mock S4 object??
I'm not seeing a class definition for any "S4"-classed object. I would expect you to have used `setClass`. I believe that once the class is defined that you should have access to the `new` constructor function.
Thanks in advance, Ramiro [[alternative HTML version deleted]]
R-help is a plain-text mailing list. -- David Winsemius Alameda, CA, USA
Yes, so are you suggesting that I create an instance of my S4 class in order to test my function. My understanding is that ideally the test should not depend on any code besides the one that I am testing. I just thought that you could perhaps define a mock class so that I would not need to invoke my external constructor function, hence making this test focus exclusively on the function that I am trying to test. Is it possible to do this in R?
On 2/1/17, 6:57 PM, "David Winsemius" <dwinsemius at comcast.net> wrote:
On Feb 1, 2017, at 11:46 AM, Ramiro Barrantes
<ramiro at precisionbioassay.com> wrote:
Hello,
I have a function that applies to an S4 object which contains a slot
called @analysis:
function calculation(myObject) {
tmp <- myObjects at analysis
result <- ...operations on analysis...
return result
}
I am writing a unit test for this function. So I was hoping to create
a mock object but I can't figure out how to do it:
test_that("test calculation function", {
mockMyObject<- mock(?????) #I am not sure what to put here
r<-calculation(mockMyObject)
expect_true(r,0.83625)
})
How can I create a mock S4 object??
I'm not seeing a class definition for any "S4"-classed object. I would expect you to have used `setClass`. I believe that once the class is defined that you should have access to the `new` constructor function.
Thanks in advance, Ramiro [[alternative HTML version deleted]]
R-help is a plain-text mailing list. -- David Winsemius Alameda, CA, USA
You seem like you have painted yourself into a corner... you want to access slots in an S4 object yet you don't want to create one. Are you going to do this test without the benefit of an operating system either? Beware of getting too meta... David didn't say you had to use an S4 object defined elsewhere... you invented that. But you do have to use the setClass function to create an S4 class with which you can create an object. No class, no object, mock or otherwise. Me, I like S3.
Sent from my phone. Please excuse my brevity.
On February 2, 2017 11:58:52 AM PST, Ramiro Barrantes <ramiro at precisionbioassay.com> wrote:
>Yes, so are you suggesting that I create an instance of my S4 class in
>order to test my function.
>
>My understanding is that ideally the test should not depend on any code
>besides the one that I am testing.
>
>I just thought that you could perhaps define a mock class so that I
>would not need to invoke my external constructor function, hence making
>this test focus exclusively on the function that I am trying to test.
>Is it possible to do this in R?
>
>On 2/1/17, 6:57 PM, "David Winsemius" <dwinsemius at comcast.net> wrote:
>
>>
>>> On Feb 1, 2017, at 11:46 AM, Ramiro Barrantes
>>><ramiro at precisionbioassay.com> wrote:
>>>
>>> Hello,
>>>
>>> I have a function that applies to an S4 object which contains a slot
>>>called @analysis:
>>>
>>> function calculation(myObject) {
>>> tmp <- myObjects at analysis
>>> result <- ...operations on analysis...
>>> return result
>>> }
>>>
>>> I am writing a unit test for this function. So I was hoping to
>create
>>>a mock object but I can't figure out how to do it:
>>>
>>> test_that("test calculation function", {
>>> mockMyObject<- mock(?????) #I am not sure what to put here
>>> r<-calculation(mockMyObject)
>>> expect_true(r,0.83625)
>>> })
>>>
>>> How can I create a mock S4 object??
>>
>>I'm not seeing a class definition for any "S4"-classed object. I would
>>expect you to have used `setClass`. I believe that once the class is
>>defined that you should have access to the `new` constructor function.
>>
>>>
>>> Thanks in advance,
>>> Ramiro
>>>
>>> [[alternative HTML version deleted]]
>>
>>R-help is a plain-text mailing list.
>>
>>--
>>
>>David Winsemius
>>Alameda, CA, USA
>>
>
>
>______________________________________________
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.
On 02/01/2017 02:46 PM, Ramiro Barrantes wrote:
Hello,
I have a function that applies to an S4 object which contains a slot called @analysis:
function calculation(myObject) {
tmp <- myObjects at analysis
result <- ...operations on analysis...
return result
}
I am writing a unit test for this function. So I was hoping to create a mock object but I can't figure out how to do it:
test_that("test calculation function", {
mockMyObject<- mock(?????) #I am not sure what to put here
r<-calculation(mockMyObject)
expect_true(r,0.83625)
})
How can I create a mock S4 object??
I don't know of a convenient way to create a mock with functionality
like mocks in other languages. But here's a class
.A = setClass("A", contains="integer")
This creates an instance that might be used as a mock
mock = .A() # same as new("A")
but maybe you have an initialize method (initialize methods are very
tricky to get correct, and many people avoid them, using
plain-old-functions to form an API around object creation; the
plain-old-function finishes by calling the constructor .A() or new("A"))
that has side effects that are inappropriate for your test, mimicked
here with stop()
setMethod("initialize", "A", function(.Object, ...) stop("oops"))
our initial attempts are thwarted
> .A()
Error in initialize(value, ...) : oops
but we could reach into our bag of hacks and try
mock = .Call(methods:::C_new_object, getClassDef("A"))
You would still need to populate slots / data used in your test, e.g.,
slot(mock, ".Data") = 1:4
This is robust to any validity method, since the validity method is not
invoked on direct slot assignment
setValidity("A", function(object) {
if (all(object > 0)) TRUE else "oops2"
})
slot(mock, ".Data") = 0:4 # still works
So something like
mockS4object = function(class, ..., where=topenv(parent.frame())) {
obj <- .Call(
methods:::C_new_object,
getClassDef(class, where=where)
)
args = list(...)
for (nm in names(args))
slot(obj, nm) = args[[nm]]
obj
}
mockS4object("A", .Data=1:4)
Mock objects typically have useful testing properties, like returning
the number of times a slot (field) is accessed. Unfortunately, I don't
have anything to offer for that.
Martin
Thanks in advance, Ramiro [[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
This email message may contain legally privileged and/or...{{dropped:2}}
Thank you so much!! This is very helpful.
On 02/01/2017 02:46 PM, Ramiro Barrantes wrote:
Hello,
I have a function that applies to an S4 object which contains a slot called @analysis:
function calculation(myObject) {
tmp <- myObjects at analysis
result <- ...operations on analysis...
return result
}
I am writing a unit test for this function. So I was hoping to create a mock object but I can't figure out how to do it:
test_that("test calculation function", {
mockMyObject<- mock(?????) #I am not sure what to put here
r<-calculation(mockMyObject)
expect_true(r,0.83625)
})
How can I create a mock S4 object??
I don't know of a convenient way to create a mock with functionality
like mocks in other languages. But here's a class
.A = setClass("A", contains="integer")
This creates an instance that might be used as a mock
mock = .A() # same as new("A")
but maybe you have an initialize method (initialize methods are very
tricky to get correct, and many people avoid them, using
plain-old-functions to form an API around object creation; the
plain-old-function finishes by calling the constructor .A() or new("A"))
that has side effects that are inappropriate for your test, mimicked
here with stop()
setMethod("initialize", "A", function(.Object, ...) stop("oops"))
our initial attempts are thwarted
> .A()
Error in initialize(value, ...) : oops
but we could reach into our bag of hacks and try
mock = .Call(methods:::C_new_object, getClassDef("A"))
You would still need to populate slots / data used in your test, e.g.,
slot(mock, ".Data") = 1:4
This is robust to any validity method, since the validity method is not
invoked on direct slot assignment
setValidity("A", function(object) {
if (all(object > 0)) TRUE else "oops2"
})
slot(mock, ".Data") = 0:4 # still works
So something like
mockS4object = function(class, ..., where=topenv(parent.frame())) {
obj <- .Call(
methods:::C_new_object,
getClassDef(class, where=where)
)
args = list(...)
for (nm in names(args))
slot(obj, nm) = args[[nm]]
obj
}
mockS4object("A", .Data=1:4)
Mock objects typically have useful testing properties, like returning
the number of times a slot (field) is accessed. Unfortunately, I don't
have anything to offer for that.
Martin
Thanks in advance,
Ramiro
[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
This email message may contain legally privileged and/or confidential information. If you are not the intended recipient(s), or the employee or agent responsible for the delivery of this message to the intended recipient(s), you are hereby notified that any disclosure, copying, distribution, or use of this email message is prohibited. If you have received this message in error, please notify the sender immediately by e-mail and delete this email message from your computer. Thank you.