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
oracle11g
minimum-spanning-tree
kde
vmware
bpmn
sqlite-net-extensions
yarn
sql-server-2016
ojdbc
pivotal-cloud-foundry
upload
cloudkit
spring-kafka
midi
wheelnav.js
msp430
quartz-scheduler
ghc
libtiff
solaris-10
autoconf
scriptcs
dcevm
lcd
java-7
dynamic-featured-image
status
virtualdub
maxmind
spring-mybatis
xacml
procdump
flink-streaming
javascriptcore
iframe-resizer
google-qpx-express-api
copying
mozilla
language-concepts
nssegmentedcontrol
main
event-driven
objectlistview
theano.scan
unobtrusive-validation
ansible-playbook
streamreader
ibpy
isbn
http-redirect
btrace
galleria
wso2carbon
auto-update
komodoedit
prolog-setof
theming
return-value
np-complete
r-forge
python-cryptography
epson
gnome-shell-extensions
home-directory
url-masking
multi-level
qpid
python-stackless
energy
ford-fulkerson
unity-networking
sdhc
quicklisp
security-testing
muse
tableau-online
mmc
unity5.2.3
sonarqube5.1.2
proj4js
notify
tt-news
dml
fluentautomation
picturefill
hyprlinkr
monomac
gnu-smalltalk
pyhdf
page-layout
interface-orientation
sabredav
tinn-r
bitsharp
bluepill
gdata-api
runas
jspinclude
online-compilation
qtkit
uiviewanimation-curve
nsmanagedobject
semantic-diff
hirefire
table-footer
lpeg
wise
nhibernate.search
preference
webkit.net
adrotator
defensive-programming

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