Does anyone have any experience getting MSTest to copy hibernate.cfg.xml properly to the output directory? All my MSTests fail with a cannot find hibernate.cfg.xml error (I have it set to Copy Always), but my MBUnit tests pass.
问题:
回答1:
You can try adding the DeploymentItemAttribute to one of your tests, or edit your .testrunconfig file and add the file to the Deployment list.
回答2:
Edit localtestrun.testrunconfig (in your solution items folder). Select the deployment option and add the hibernate.cfg.xml file to the list of additional files to deploy. The file should then get copied to the output directory where the test gets run.
回答3:
Ran into the same thing a few weeks ago -- this is actually a bug with MSTest -- I believe this was corrected with the recent Service Pack Release (even though it still says "Active"). If not, all I had to do was reference my hibernate.cfg.xml directly (sloppy but works for testing -- this is referencing the hibernate.cfg.xml file in my tests project from the "TestResults" folder):
try
{
sessionFactory = new Configuration()
.Configure()
.BuildSessionFactory();
}
// Assume we are in "MSTest mode"
catch (Exception)
{
sessionFactory = new Configuration()
.Configure(@"..\..\..\Program.Tests\" + @"\hibernate.cfg.xml")
.BuildSessionFactory();
}
回答4:
a workaround rather than an answer: NHibernate supports programmatic configuration. so you can write your own native properties/config file and parse it into hibernate configurations on startup.
回答5:
I like to mark my NHibernate config files as Embedded Resources, and use the Configuration.Configure() overload which reads config files from the Assembly Resources.