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
cakephp
log4j
testng
asp.net-core
wso2
netbeans
reflection
deezer
single-sign-on
kde
google-api-php-client
electron
framework7
elm
jxls
installshield
podio
contact
basic
slurm
dax
imacros
u-sql
iron-router
carthage
clearcase-ucm
vaadin7
saxon
text-rendering
firefox-webextensions
grails3
immutable.js
reverse-proxy
mps
telerik-reporting
smb
vlsi
react-css-modules
internet-explorer-9
social-media
replaceall
windowbuilder
gitignore
google-cloud-nl
newline
http-status-code-503
scorm2004
catel
catalog
nssegmentedcontrol
fusionpbx
qwerty
service-discovery
temporary-files
dism
powershell-dsc
vao
outlook-api
netcdf4
angular2-meteor
dds
jquery-nestable
promela
python-webbrowser
namecoin
jspdf-autotable
cubes
django-debug-toolbar
bstr
firebaseui
contact-list
multiple-regression
flash-cs5
simplewebrtc
historian
freedesktop.org
lib.web.mvc
teamcity-8.0
ios9.1
fluid-dynamics
remobjects
apache-commons-net
operator-precedence
gwidgets
knuth
openexr
contenttype
expected-exception
cloud-connect
centos5
ivyde
.aspxauth
tws
gridcontrol
free-variable
specification-pattern
pysimplesoap
labwindows
hosts-file
bubble-chart
osql
ohm
pydot
deobfuscation
blackberry-playbook
ecl
pyinotify
jmock
telerik-scheduler
wise
filtered-index
perfect-hash
multi-tier
temporal-database
commodore
windows-live-messenger
rtml

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App