Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False positive for FakeItEasy0003 when using argument constraint in CallToSet with indexer property #41

Closed
zivarah opened this issue Jan 19, 2023 · 4 comments · Fixed by #42
Labels
bug Something isn't working contribution
Milestone

Comments

@zivarah
Copy link

zivarah commented Jan 19, 2023

Version: FakeItEasy.Analyzer.CSharp 6.1.0

Consider this simple class:

using System.Collections.Generic;

namespace FakeItEasyAnalyzerReproduction
{
	public class MyClass
	{
		private readonly Dictionary<string, int> _dict = new();
		public virtual int this[string key]
		{
			get => _dict.TryGetValue(key, out int value) ? value : default;
			set => _dict[key] = value;
		}
	}
}

And this simple test:

using FakeItEasy;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace FakeItEasyAnalyzerReproduction.Test
{
	[TestClass]
	public class MyClassTests
	{
		[TestMethod]
		public void TestMethod1()
		{
			MyClass instance = A.Fake<MyClass>();
			A.CallTo(() => instance[A<string>._]).CallsBaseMethod();

			// This line throws FakeItEasy0003 warning, but this call configuration works as intended
			// and the tests won't pass without it
			A.CallToSet(() => instance[A<string>._]).CallsBaseMethod();

			// Also works, and does not throw any warnings
			A.CallToSet(() => instance["key3"]).Throws(() => new Exception());

			instance["key1"] = 1;
			instance["key2"] = 2;
			Assert.AreEqual(1, instance["key1"]);
			Assert.AreEqual(2, instance["key2"]);
			Assert.ThrowsException<Exception>(() => instance["key3"] = 3);
		}
		}
	}
}

Expected behavior: No warning for this code
Actual behavior: FakeItEasy0003 warning for A.CallToSet(() => instance[A<string>._]).CallsBaseMethod();

@zivarah
Copy link
Author

zivarah commented Jan 19, 2023

FYI that I plan to tackle this myself, hopefully in the next day or two.

@thomaslevesque
Copy link
Member

Hi @zivarah,
Nice catch! Thanks for giving it a shot. Let us know if you need any help!

zivarah pushed a commit to zivarah/FakeItEasy.Analyzers that referenced this issue Jan 20, 2023
zivarah pushed a commit to zivarah/FakeItEasy.Analyzers that referenced this issue Jan 20, 2023
@blairconrad blairconrad added the bug Something isn't working label Jan 20, 2023
thomaslevesque added a commit that referenced this issue Jan 20, 2023
…nstraint-in-calltoset

Resolve #41: False positive for FakeItEasy0003 when using argument constraint in CallToSet with indexer property
@blairconrad blairconrad added this to the vNext milestone Jan 20, 2023
@github-actions
Copy link

This change has been released as part of FakeItEasy.Analyzers 6.1.1.

@blairconrad
Copy link
Member

Thanks again, @zivarah. You should be able to upgrade to 6.1.1 now and reap your own benefits.

🥇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working contribution
Projects
None yet
3 participants