fakeiteasy


FakeItEasy setting property with no get accessor?


I'm trying to use FakeItEasy 2.0.0 to fake a property in a simple interface:
public interface IPerson
{
int Age { set; }
}
Note that I don't have a get accessor. The test I'm trying to write is:
public void SetsAge()
{
var fakePerson = A.Fake<IPerson>();
A.CallToSet(() => fakePerson.Age).To(42).MustHaveHappened();
fakePerson.Age = 42;
}
But the line containing A.CallToSet fails to compile with:
which is fairly self-explanatory, but confusing since I'm not trying to get the property's value.
Do I have to provide a get accessor to get this to compile (even though I don't want a get accessor)? What is the reason that it requires the get accessor in this case (the same compiler error happens when I replace MustHaveHappened with DoesNothing)? Or am I doing something fundamentally wrong?
Or perhaps I shouldn't lose too much sleep over this and do the right thing in the first place?
Do I have to provide a get accessor to get this to compile?
No, you can use
A.CallTo(fakePerson).Where(call => call.Method.Name == "set_Age" &&
call.GetArgument<int>(0) == 42)
.MustHaveHappened();
This is documented in Specifying a call to any method or property.
What is the reason that it requires the get accessor?
The reason is that because you can't use a = in a lamdba expression, there's no easy way to refer to the property setter. In 2.0, we added A.CallToSet to allow you to cheat by using the getter, but of course it only works when there is a getter.
We've not yet come up with an elegant way to refer to a getterless setter, so you have to use the powerful version of A.CallTo above.
Or am I doing something fundamentally wrong?
Well, in addition to the problem with referring to the property, the whole A.CallTo…MustHaveHappend() has to occur after fakePerson.Age = 42, or it will report a failure, because you haven't yet set fakePerson.Age to 42.

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
ms-access
log4j
google-chrome-extension
listview
pypi
mockito
dictionary
google-api-php-client
nullpointerexception
syntax
dot
yahoo-oauth
leon
infragistics
sql-server-2016
bs4
qore
cloudkit
session-variables
adobe-analytics
izpack
floating-action-button
u-sql
gz
windows-phone-7
vaadin7
invantive-sql
dbext
kryo
accessor
smb
shopware
sparse-matrix
pepper
dynamic-featured-image
info.plist
procdump
srcset
karaf
http-get
y86
sequential
restlet
mixture-model
event-driven
service-discovery
opshub
ncalc
ruby-on-rails-3.1
grid.mvc
janrain
nomethoderror
logfiles
avconv
azure-application-gateway
termination
powercli
statsd
mplayer
drupal-6
windows-iot-core-10
django-scheduler
minimization
account-kit
pintos
yt-project
android-fonts
instant
slicknav
asp.net-4.5
websitepanel
freelancer.com-api
natvis
vhd
srand
mmc
lustre
umbraco6
dd
gui-test-framework
java.util.concurrent
device-manager
responsive-slides
comexception
java.nio.file
quantlib-swig
ril
cos
shellexecute
gridcontrol
rabl
random-seed
sqlperformance
kyotocabinet
path-separator
unc
quickdialog
bluepill
automount
pendrive
cufon
data-loss
coredump
krl
jmock
lang
h.323
dmx512
jquery-ui-droppable
nsviewanimation
mtj
sector
spec#
scripting-languages
w3wp
geneva-server
lzh

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