0

I am using NUnit with Visual Studio Express Edition 2010 for C#, Now, normally test works fine. But whenever I try to use Massive.cs, which is open source api to access database. Test fails from that file only. Now, if I run the application, api is working fine. I have created a different library file to access data base.

I seriously don't understand the error. It is just giving error that object reference is not set to an object. But if I run the code, it works fine. I am using dynamic keyword as shown in link of api above. Does that making problem with NUnit ?

Is there any other way to test in this type of Scenarios?

Here are the further details of the code,

Test class is like this

dynamic item = new Item();
item.Insert(new { Name = "Maggi", Description = "Its 2 Min Nuddles", IsDelete = false });

var items = item.All();

Assert.AreEqual("Maggi", items.FirstOrDefault().Name);

Now, I have put test here. Which gives error like shown in image, Error coming while testing with NUnit

Now if I run code in console application, then code is working fine, code snippet is given below

dynamic item = new Item();
        item.Insert(new { Name = "Maggi", Description = "Its 2 Min Nuddles", IsDelete = false });


        var result = item.All();

        foreach (var i in result)
        {
            Console.WriteLine(i.Name + i.Description);
        }

        Console.Read();

Here, code is working and same thing is not working with NUnit Test. Please have a look and help me out. Please let me know if any further information is needed from my side.

kunjee
  • 2,739
  • 1
  • 23
  • 38

2 Answers2

0

Most probable explanation is that you haven't set up your connection string in the test project.
If you are using NUnit, just put it in app.config of your test project.

Andrey Shchekin
  • 21,101
  • 19
  • 94
  • 162
  • I have set connection string in test project, that's why other code is running, its in test project only. I put that to check, this connection issue. Still I cross check and get back. – kunjee Nov 22 '11 at 04:20
  • Ok, can you set `Debug -> Exceptions -> Common Language Runtime Exceptions` to break when thrown, run test under the debugger, and see what exactly is `null`? – Andrey Shchekin Nov 22 '11 at 04:45
  • Note working. Have you checked the API. Error is while creating constructor, public class derivedClass : DynamicModel { public derivedClass() : base("ExpenseManager", "Income", "Id") { } } when this ctor called, its gives error, shown in image above. – kunjee Nov 22 '11 at 05:12
  • Finally found it, I guess. Its dynamic what I am declaring near Item. while creating object. Because of it, its not giving enumerable, until and up till I start looping. Its works run time I guess. I convert it to var which is compile time. if I am using var it is working with operation like first and firstordefault. But still test is failing. Unit test can't initiate dynamic thing, I guess. – kunjee Nov 22 '11 at 06:25
  • There is nothing in tests that disallows `dynamic` to be used, you just have to debug what exactly is `null`, without that it is hard to help. – Andrey Shchekin Nov 22 '11 at 21:03
  • You are right. Dynamic is allowed and working perfectly with nunit. But the problem is code is also executing perfectly. Still I am checking it out, will get back once we found the issue. – kunjee Dec 01 '11 at 03:51
0

Solved... There is a issue with NUnit Testing. It was not taking config file pefectly. So, I made two changes. Changes I have done in Project setting.

First change is to change Application Base to bin\debug just give application base as this and then config file to .config to .exe.config and things are up and running. :)

kunjee
  • 2,739
  • 1
  • 23
  • 38