question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

No way to assert a call to a setter wasnt made.

See original GitHub issue

Big fan of framework, found a small annoyance:

I’d like to assert that a property setter was not called. I’m looking at your documentation, and I cant find a way to do this.

        private class IntBox
        {
            public int IntProperty { get; set; }
        }

        [Test]
        public void when_using_A_callTo_on_a_property_set_call()
        {
            var fake = A.Fake<IntBox>();
            fake.IntProperty = 4;

            A.CallTo(() => fake.IntProperty).WithAnyArguments().MustNotHaveHappened();
            //ahhh so set calls are excluded...

//            A.CallTo(() => fake.IntProperty = -1).WithAnyArguments().MustHaveHappened();
            //and of course an expression cannot contain an assignment operator...
        }

I would argue that the expected behavior is that

A.CallTo(() => fake.IntProperty).MustNotHaveHappened()

would check for both get and set calls, but this would be a breaking change in your framework and might anger a good number of people, so perhaps the simple static methods Get and Set on IReturnValueArgumentValidationConfiguration<T> would suffice, so that my assertion could be:

A.CallTo.Get(() => fake.IntProperty).MustNotHaveHappened()
A.CallTo.Set(() => fake.IntProperty).MustNotHaveHappened()

Cheers!

-Geoff

Issue Analytics

  • State:closed
  • Created 11 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
Jameskmongercommented, Feb 24, 2016

I can confirm that @patrik-hagne 's solution above works correctly.

The “set” value is passed as the first argument, so you can do this to match the arguments (for example, to check that “IntProperty” is set to 4:

A.CallTo(fake)
    .Where(x => x.Method.Name.Equals("set_IntProperty"))
    .WhenArgumentsMatch(x => x.Get<int>(0).Equals(4))
    .MustHaveHappened();
1reaction
patrik-hagnecommented, Jan 25, 2013

You can do it this way (from the top of my head):

A.CallTo(fake).Where(x => x.Method.Name.Equals("set_IntProperty")).WithAnyArguments().MustNotHaveHappened();
Read more comments on GitHub >

github_iconTop Results From Across the Web

Rhino Mocks 3.5 Test that property setter was not called
Set the property to some known value in the test. Call the code that won't change the property, then assert that the property...
Read more >
Checking received calls
The Received() extension method will assert that at least one call was made to a member, and DidNotReceive() asserts that zero calls were...
Read more >
How to spy on a property (getter or setter) with Jasmine
As I mentioned earlier, it is just like spyOn so we can assign its returned reference to a new variable, we can stub...
Read more >
java - Unit testing when you have no getters and setters
My question is how can you do unit tests for this class. Consider the following example: public class Account { private Money money;...
Read more >
Avoid getters and setters whenever possible
The best way, though, is to not expose the member in any way at all and instead bring the behavior that manipulates the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found