需要帮助NUnit [英] Need help with NUnit

查看:98
本文介绍了需要帮助NUnit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi


我正在测试一些测试软件NUnit和TDD。虽然我可以毫无问题地让TDD工作但我很难获得NUnit的工作资产。



下面是我正在运行测试的类的简化版本。

  public   class  buildings:RatingBase 
{
public 覆盖 十进制 Premium()
{
// 已将其剥离以仅显示导致错误的区域
decimal TAMultipler = TA();
}

private decimal TA()
{
string regions = RegionResults(Postcode);
}
}

public abstract class RatingBase
{
public string RegionResults( string 邮编, string Town =
{
using (DataContext db = new DataContext(ConfigurationManager.ConnectionStrings [ < span class =code-string> connectionstring]。ToString())
{
}

}
}





现在在Microsoft TDD下它按预期失败/传递断言,取决于我输入的值与<的结果进行比较code> Premium()



但是当我尝试使用以下测试的NUnit时,这与TDD中使用的测试相同尝试。我已尝试使用datacontext的模拟和没有,结果仍然是相同的。



 [TestFixture] 
public class TestBuildings
{
[Test]
< span class =code-keyword> public void CalculateBuidings()
{
// string connection =Data Source = oak_apps; Initial Catalog = Oak_Underwriting; Integrated Security = True;
// 模拟< DataContext> db = new Mock< DataContext>(连接);

建筑物blds = new Buildings();
blds.Postcode = TN20 6JS;
blds.Age = 54 ;
blds.Occupation = 保险公司董事;
blds.PropertyType = Detached;
blds.YearBuilt = 2012 ;
blds.SumInsured = 2878500 ;
blds.Excess = 500 ;

blds.PercentageAdjustment = 0 .855M;
blds.MoneyAdjustment = 0 ;
blds.Discretionary = 10 ;

decimal value = blds.Premium();

Assert.AreEqual( 1792 .809M, value );
}
}





当我通过Nunit测试应用程序运行它时总是会遇到以下错误



Rating.TestBuildings.CalculateBuidings:

System.NullReferenceException:对象引用未设置为对象的实例。



我错过了在NUnit测试中需要设置的东西吗?任何使它工作的指针都会很棒!



谢谢

Simon

解决方案

运行NUnit时使用的app.config是什么样的?



这听起来非常像是缺少配置的情况,请尝试确保配置你需要存在于测试项目的app.config中。



希望这会有所帮助,

Fredrik


< blockquote>好的,基于此 - 两件事。



首先,我怀疑你可能正在使用NUnit测试运行器,所以我认为它会查看那个东西的app.config用于连接字符串而不是app。这就解释了为什么它适用于微软的方法。



其他的东西(也许并不重要):



使用(DataContext db = new DataContext(ConfigurationManager.ConnectionStrings [connectionstring]。ToString())





应该是:



使用(DataContext db = new DataContext(ConfigurationManager.ConnectionStrings [connectionstring]。ConnectionString)





无论哪种方式,我打赌你在null上调用ToString()!


Hi
Im testing out some testing software well NUnit and TDD. while I can get TDD to work with no problem I am struggling to get NUnit working property.

Below is a simplified version of the class that I am running the test on.

public class buildings : RatingBase
{
  public override decimal Premium()
  {
    //have stripped this down to show only the area that is causing the error
    decimal TAMultipler = TA();
  }

  private decimal TA()
  {
    string regions = RegionResults(Postcode);
  }
}

public abstract class RatingBase
{
  public string RegionResults(string Postcode, string Town = "")
  {
    using(DataContext db = new DataContext(ConfigurationManager.ConnectionStrings["connectionstring"].ToString())
    {
    }
  }
}



Now under Microsoft TDD it works as expected fails / passes on the assertion depending on the values I put in to compare against the result of Premium()

But when I try NUnit with the following test, which is the same test used in the TDD attempt. I have tried it with both the mock of the datacontext and without and the result is still the same.

[TestFixture]
public class TestBuildings
{
    [Test]
    public void CalculateBuidings()
    {
        //string connection = "Data Source=oak_apps;Initial Catalog=Oak_Underwriting;Integrated Security=True";
        //Mock<DataContext> db = new Mock<DataContext>(connection);

        Buildings blds = new Buildings();
        blds.Postcode = "TN20 6JS";
        blds.Age = 54;
        blds.Occupation = "Insurance Company Director";
        blds.PropertyType = "Detached";
        blds.YearBuilt = 2012;
        blds.SumInsured = 2878500;
        blds.Excess = 500;

        blds.PercentageAdjustment = 0.855M;
        blds.MoneyAdjustment = 0;
        blds.Discretionary = 10;

        decimal value = blds.Premium();

        Assert.AreEqual(1792.809M, value);
    }
}



When I run it though Nunit test application it always throughs the following error

Rating.TestBuildings.CalculateBuidings:
System.NullReferenceException : Object reference not set to an instance of an object.

Am I missing something that I need to setup in the NUnit test? any pointers to get it working would be great!

Thanks
Simon

解决方案

What does the app.config that's used when running NUnit look like?

This sounds very much like a case of missing configuration, try making sure the configuration you need exists in the app.config of the test project.

Hope this helps,
Fredrik


Ok, based on that - two things.

Firstly, I suspect that you may be using NUnit test runner, so I think it will be looking in that thing's app.config for the connection string rather than you app. That'd explain why it works with Microsoft's approach.

Other thing (maybe not important):

using(DataContext db = new DataContext(ConfigurationManager.ConnectionStrings["connectionstring"].ToString())



Should be:

using(DataContext db = new DataContext(ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString)



Either way, I bet that you are calling ToString() on a null!


这篇关于需要帮助NUnit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆