Pergunta

I have a WCF service that is a facade for a VB application. My VB application expects an ADODB.Recordset as my input parameter. I have written an unit test for this method but i don't know how to manually create a recordset for testing purposes.

I came up with this (after long searches on the web) :

ADODB.Recordset tempRS = new ADODB.Recordset();
tempRS.Fields.Append("Field1", ADODB.DataTypeEnum.adChar);  
tempRS.Fields.Append("Field2", ADODB.DataTypeEnum.adInteger);

but this recordset does not have data in it. Or another try :

ADODB.Recordset temp = new ADODB.Recordset();
temp.Open();
temp.AddNew("Field1", "data for field 1");
temp.AddNew("Field2", "1");

but i get an error on the temp.Open() statement saying The connection cannot be used to perform this operation. It is either closed or invalid in this context.

Can someone point me in the right direction ? I have read on msdn but didn't find an example that suits my needs.

Thanks in advance !

Foi útil?

Solução

I have personally written C# unit tests in which I have successfully instantiated a real ADODB.Recordset and populated it with data, so I can assure you that it is possible.

The key is to open the recordset in disconnected mode. This gives you the ADO equivalent of a DataTable, and does not require a db connection. You can do this by explicitly setting the cursor type to static, e.g.

temp.Open(CursorType: CursorTypeEnum.adOpenStatic);

NOTE: Static cursors can be used in connected mode as well. However, they are the only cursor type that makes sense in disconnected mode, which is why you need to specify it explicitly.

Outras dicas

If someone else gets into this error, i solve it using the following code :

private ADODB.Recordset createRecordSet()
    {            
        ADODB.Recordset tempRS = new ADODB.Recordset();
        tempRS.Fields.Append("Field1", DataTypeEnum.adVarChar, 255);
        tempRS.Fields.Append("Field2", ADODB.DataTypeEnum.adInteger, 5);            
        tempRS.Open(CursorType: CursorTypeEnum.adOpenStatic);

        tempRS.AddNew();
        tempRS.Fields["Field1"].Value = "test from UnitTest";
        tempRS.Fields["Field2"].Value = 1;
        return tempRS;
    }

It seems to me when you do,,,,

ADODB.Recordset temp = new ADODB.Recordset(); temp.Open();

it looks for a connection and you get an error because the connection is closed/not exist. Of course with Unit tests you want to avoid talking to any configuration or external systems. If you do want, then it would be an integration test.

What you can do is to couple of fake implementations that mimic the behaviour of the record set. This is only during the test execution. During the actual app execution you would use a real record set.

The way you wire this up would be something like below. You need to introduce couple of interfaces as below.

Create an Wrapper Interface around your VB ADODB Record Set object. It would have bunch of methods which allows you to manipulate the real record set. Something like below.

 public interface IRecordSet {
    void Open();
    void AddNew();
    void Add(string key, object value);
 }

Create a factor the allows you to have access to an implementation of an IRecordSet. Something like below

  public interface IRecordSetFactory {
    IRecordSet Create();
  }

  public class RecordSetFactory : IRecordSetFactory {
    public IRecordSet Create()
    {
        return new ADODB.Recordset();
    }
 }

Your SUT (System Under Test would use the RecordSetFactory and allows you create a new ADODB.Recordset()

Now during your Unit Testing you can change the real behaviour as desired.

In your test area create a Stub/Fake Record Set instead of the real one (Since you haven't stated that you are using any isolation/mock object framework, you can simply roll out a hand written stub as below.)

public class StubRecordSetFactory : IRecordSetFactory
{
    public IRecordSet Create() {
        return new StubRecordSet();
    }
}

Your Test can now use the StubRecordSet

[TestMethod]
public void YourWhatEverTest()
{
    var factory = new StubRecordSetFactory ();
    var stubRecordSet = factory.Create();

    //call SUT
    //...

    //Any asserts/verifications
    //...
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top