fakeiteasy


How can I fake a Class used insite SUT using FakeItEasy


Am having a little trouble understanding what and what cannot be done using FakeItEasy. Suppose I have a class
public class ToBeTested{
public bool MethodToBeTested(){
SomeDependentClass dependentClass = new SomeDependentClass();
var result = dependentClass.DoSomething();
if(result) return "Something was true";
return "Something was false";
}
}
And I do something like below to fake the dependent class
var fakedDepClass = A.Fake<DependentClass>();
A.CallTo(fakedDepClass).WithReturnType<bool>().Returns(true);
How can i use this fakedDepClass when am testing MethodToBeTested. If DependentClass was passed as argument, then I can pass my fakedDepClass, but in my case it is not (also this is legacy code that I dont control).
Any ideas?
Thanks
K
Calling new SomeDependentClass() inside MethodToBeTested means that you get a concrete actual SomeDependentClass instance. It's not a fake, and cannot be a FakeItEasy fake.
You have to be able to inject the fake class into the code to be tested, either (as you say) via an argument to MethodToBeTested or perhaps through one of ToBeTested's constructors or properties.
If you can't do that, FakeItEasy will not be able to help you.
If you do not have the ability to change ToBeTested (and I'd ask why you're writing tests for it, but that's an aside), you may need to go with another isolation framework. I have used TypeMock Isolator for just the sort of situation you describe, and it did a good job.

Related Links

Converting an MOQ property setup to FakeItEasy
Howto loop OrderedAssertions in FakeItEasy 2
FakeItEasy setting property with no get accessor?
FakeItEasy mocked method won't return object, instead nullReferenceException
How to specify the class to use when faking a property?
How can I fake a Class used insite SUT using FakeItEasy
FakeItEasy expectation fail against HashSet comparisons
FakeItEasy AssignsOutAndRefParameters - lazily?
How do I find the underlying Type of a Fake object returned from FakeItEasy?
FakeItEasy ReturnsLazily with out parameter
how to verify that a method was called with an argument of a specific type
How to fake delegates with FakeItEasy
FakeItEasy & “params” arguments
Using Expression Trees as an argument constraint
How to update a property on a parameter using FakeItEasy

Categories

HOME
ibm-bluemix
mockito
heroku
comparison
view
sd-card
amazon-ecs
google-project-tango
retrofit
datastax-java-driver
bs4
contact
medical
modx-revolution
php-7.1
n-gram
ghc
django-admin
angular-ui
reactcsstransitiongroup
textfield
immutable.js
sox
tibco-mdm
java-7
emgucv
pingfederate
fopen
instant-messaging
bosh
preg-match-all
exuberant-ctags
grails-3.1
uiswipegesturerecognizer
fusionpbx
suricata
webix-treetable
uft-api
galen
pdf-reactor
google-closure
import-from-excel
netcdf4
wptoolkit
cubic-spline
google-datalayer
xml-documentation
komodoedit
pnotify
datastax-startup
pebble-watch
integrity
yii2-extension
elgg
drupal-6
forever
epson
color-profile
mongocsharpdriver
gridview-sorting
packagist
captivenetwork
jfugue
separator
wyam
ios9.1
graph-drawing
sniffer
key-management
truevault
csquery
offloading
jsonpickle
network-interface
javafx-webengine
android-radiobutton
funcunit
app42
google-cloud-save
eventual-consistency
mylyn
yui-compressor
enterprisedb
quickdialog
dice
chrono
data-loss
vdsp
mod-auth
xmlspy
lpeg
xsdobjectgen
ncqrs
iweb
backcolor
mtj
virtual-functions
yagni
mediarss
mdac
geneva-server

Resources

Encrypt Message