1

I want to write a Unit test for verifying version of dll used in all csproj files of a solution is correct or not. Ex. i have a dll reference in 1 csproj file like this

<Reference Include="Moq, Version=4.2.1408.511, Culture=neutral, PublicKeyToken=69f491c39445e920, processorArchitecture=MSIL">
  <SpecificVersion>False</SpecificVersion>
  <HintPath>..\..\packages\Moq.4.2.1408.0511\lib\net40\Moq.dll</HintPath>
</Reference>

so here i want to read Moq version as 4.2.1408.511 and compare with "4.2.1408.511" string. How can i write unit Test to do this

varun
  • 201
  • 1
  • 5
  • 13
  • Can you not just open the csproj files as XML and then xpath into the `Reference` element and read the `Include` attribute and parse the version yourself? (and by the way, I think that's a very weird thing to test but I'm sure you have your reasons). – kha Sep 10 '15 at 07:27
  • ok kha, let me try this. – varun Sep 10 '15 at 07:31
  • if not.. just build a regex to get your information. [here](http://www.regex101.com)'s a nice help-site – Jan Unld Sep 10 '15 at 07:35

1 Answers1

0

I think this test isn't unit, it's integration. Anyway, you can check version of an assembly in this way:

var anyTypeOfAssembly = typeof(Mock<>);
var targetAssembly = anyTypeOfAssembly.Assembly;
var versionInfo = FileVersionInfo.GetVersionInfo(targetAssembly.Location);
var fileVersion = versionInfo.FileVersion;
Assert.AreEqual("4.2.1408.511", fileVersion);
Mark Shevchenko
  • 7,937
  • 1
  • 25
  • 29
  • Note that file version may not match assembly version (one come from resource info, another from assembly metadata - http://stackoverflow.com/questions/2221598/fileversioninfo-getversioninfo-incorrect-in-console-application). – Alexei Levenkov Sep 10 '15 at 07:43
  • Thnx Mark, it is working to get dll version of an assembly, but i modified csproj file with below version instead of 4.2.1408.511 i modified with 4.2.1408.512 but still Test is passing it is checking Moq dll 4.2.1408.511 Test should fail but it is passing – varun Sep 10 '15 at 07:58
  • @varun As I can see there isn't version 4.2.1408.512, I found 4.2.1408.511 only. I checked two different version of Moq, and the code is worked. – Mark Shevchenko Sep 10 '15 at 08:37
  • Yes, as the version numbers may differ, how to get the one from the csproj file at runtime? – rednoyz May 08 '18 at 04:46