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
gitlab
testng
ngrx
plone
iterator
q
routes
bookshelf.js
getelementsbytagname
yahoo-oauth
gps
convolution
directx
pheatmap
basic
windows-10-universal
node-pdfkit
kentor-authservices
dax
google-cloud-spanner
messages
tostring
text-rendering
shared-hosting
karma-jasmine
riot.js
cloudhub
functional-testing
sylius
trading
crosstab
cas
linkerd
assistant
kendo-datasource
delicious-api
socialengine
pdb
google-qpx-express-api
wallpaper
file-format
adobe-premiere
libraries
withings
sqlite2
impersonation
unobtrusive-validation
grid.mvc
eigenvalue
fakeiteasy
angular2-meteor
btrace
upstart
dds
datastax-startup
feeds
nsarray
gcsfuse
redundancy
xcb
vmware-tools
gridview-sorting
jwplayer7
ubuntu-10.04
php-internals
spim
comobject
kcachegrind
riak-cs
tableau-online
graph-drawing
generic-programming
oberon
dundas
web-controls
inmobi
directoryservices
rake-task
tween
fluentautomation
dealloc
.aspxauth
dexterity
listings
bulkloader
shellexecute
yui-compressor
mqx
limejs
simba
trusted
google-email-migration
hgsubversion
stage
hirefire
xfbml
cxxtest
h.323
meego
premature-optimization
ntvdm.exe

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App