In a MSTest project I declare an internal class for a few tests. It's declared in the test project's namespace like this:
namespace Körningar.Test.Hjälpklasser
{
internal interface ITestkörningParams { int Heltal { get; } string Text { get; } }
internal class TestkörningParams : ITestkörningParams { public int Heltal { get; set; } public string Text { get; set; } }
// ...
}
In a tested method I have an instance of this class parms
, and iterate through it's properties to log its values:
// Create parms of type körningsparametrarKlass.
object parms = paramhämtare.HämtaParametrar(args, körningsparametrarKlass);
// Log the property values of the parms object.
log.Info($"Körningsparametrar:\r\n"
+ string.Join("\r\n",
körningsparametrarKlass.GetProperties()
.Where(pi => pi.GetGetMethod() != null)
.Select(pi
=> $"{pi.Name} = {Convert.ToString(pi.GetGetMethod().Invoke(parms, null))}")
)
);
This line of code throws a TargetException
with message saying that the object doesn't match the target type.
The method paramhämtare.HämtaParametrar
creates the returned object like this:
object parms = Activator.CreateInstance(körningsparametrarKlass)
// ...assign properties
return parms;
I fail to see how the parms object could possibly be the wrong type for a property getter that's extracted from körningsparametrarKlass
, which is the same type that's instantiated for the parms
object.
Examining the variables in VS Watch windows gives the following:
As you can see, the AssemblyQualifiedName is the same for the parms
object and the körningsparametrarKlass
type, but the actual type objects differ (marked in the dump). As far as I know type objects should be comparable using ==
, but I have also tested with körningsparametrarKlass.Equals(parms.GetType())
with the same result. Also note that all lines in the Watch window are actually up to date even though some are gray - I refreshed them all before taking the dump.
How can this happen, and how would i fix it?
I should also mention that this seems to happen only if I run tests for both this test project and another test project in the same solution. If I run only the test in question, or the entire test project that contains it, there is no exception.
Aucun commentaire:
Enregistrer un commentaire