1

I want to unit test my cshtml files to ensure they compile successfully as recently I've had some issues where at run time there were failures due to the views expecting properties that weren't present. I am trying to use RazorEngine to check this, and here is my attempt at a unit test:

      public void LayoutCshtml_AnyCase_ViewCompilesSuccessfully()
        {
            var model = new Model();

            var templateName = File.ReadAllText("PathToView/_Layout.cshtml");
            var result = Engine.Razor.RunCompile(templateName, "key", typeof(Model), model);
            //Assertion will go here 

        }

I will add the assertion later but at the moment just want the view to compile. The templateName correctly picks up the right .cshtml but the result fails because of error:

The predefined type 'System.Runtime.CompilerServices.ExtensionAttribute' is defined in multiple assemblies in the global alias;

Is anyone familiar with using RazorEngine to test please?

B.S.
  • 668
  • 1
  • 5
  • 15
Jordan1993
  • 864
  • 1
  • 10
  • 28
  • Could that be a discovered error? Does the page being tested work at run time? https://stackoverflow.com/questions/546819/strange-warning-about-extensionattribute – Nkosi Jan 22 '21 at 12:05
  • @Nkosi yes, works at runtime – Jordan1993 Jan 22 '21 at 12:06
  • Check the link i included in my previous comment – Nkosi Jan 22 '21 at 12:07
  • I would suggest running an integration test that calls the desired view. Should be able to get the error if the view is not able to be compiled. – Nkosi Jan 22 '21 at 12:09

0 Answers0