[R] Using a mock of an S4 class

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Thu Feb 2 23:14:48 CET 2017


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.



More information about the R-help mailing list