Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into lazy-setup-all-prop…
Browse files Browse the repository at this point in the history
…erties
  • Loading branch information
ishimko committed Jun 1, 2019
2 parents 758f421 + afa3e6c commit f7a27a6
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is loosely based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).


## Unreleased

#### Fixed

* Regression: Unhelpful exception message when setting up an indexer with `SetupProperty` (@stakx, #823)


## 4.11.0-rc1 (2019-04-19)

This is a pre-release version.
Expand Down
4 changes: 4 additions & 0 deletions src/Moq/Mock.Generic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,10 @@ public Mock<T> SetupProperty<TProperty>(Expression<Func<T, TProperty>> property,
{
Guard.NotNull(property, nameof(property));

var pi = property.ToPropertyInfo();
Guard.CanRead(pi);
Guard.CanWrite(pi);

TProperty value = initialValue;
this.SetupGet(property).Returns(() => value);
Mock.SetupSet(this, property.AssignItIsAny(), condition: null).SetCallbackResponse(new Action<TProperty>(p => value = p));
Expand Down
26 changes: 26 additions & 0 deletions tests/Moq.Tests/Regressions/IssueReportsFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2545,6 +2545,32 @@ public void SetupSequencePhraseConvertsExpressionToDescriptiveString()

#endregion

#region 823

public class Issue823
{
public interface IX
{
string this[int index] { get;set; }
}

[Fact]
public void Setting_up_indexer_with_SetupProperty_throws_with_error_message_pointing_at_the_problem()
{
var mock = new Mock<IX>();
var ex = Assert.Throws<ArgumentException>(() => mock.SetupProperty(m => m[1]));
if (!string.IsNullOrEmpty(ex.ParamName))
{
Assert.Equal("property", ex.ParamName);
}
Assert.Contains("not a property", ex.Message);
// ^ To add some context for these tests, at one point the exception thrown read:
// "Expression must be writeable", referring to a parameter "left".
}
}

#endregion

// Old @ Google Code

#region #47
Expand Down

0 comments on commit f7a27a6

Please sign in to comment.