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
ionic-framework
clips
vbscript
hp-exstream
youtube-dl
binary-tree
google-translate
gorm
maude-system
usergrid
orchardcms
ups
pugjs
reactcsstransitiongroup
viewport
sqlcipher
microsoft-sync-framework
dynamic-featured-image
status
uninstall
fifo
webkitspeechrecognition
file-rename
flink-streaming
uisplitview
subset-sum
mozilla
force-layout
catalog
wpf-controls
wixsharp
io-redirection
vapor
elasticsearch-plugin
dism
vao
reactive-cocoa-5
auto-update
jsch
scrollable
osx-mavericks
dropbox-php
git-diff
tactic
bind9
orthogonal
visual-c++-2008
plottable.js
bstr
simplewebrtc
qcustomplot
hexdump
pyke
iad
xna-4.0
msys
atk4
coveralls
sframe
android-listview
ibaction
geonetwork
expected-exception
marmalade
xceed-datagrid
commoncrypto
aqtime
dataservice
ril
gridcontrol
chronoforms
comaddin
jelly
netbeans-6.9
work-stealing
nsdatecomponents
qt-jambi
appender
recent-documents
lang
gamma
thunderbird-lightning
photoshop-cs4
privilege
phonon
gacutil
spec#

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