locked
Passing parameters between test scripts RRS feed

  • Question

  • I'm trying to set up my unit tests so that the data setup and results checking happens in the pre- and post-test scripts where the PrivilegedContext runs, and the stored procedure execution occurs in the Test script where the ExecutionContext runs.  It seems this was Microsoft's intent so we could run the unit test under a non-privileged account to verify security settings.

    I've created a DbParameter array in the code-behind and it's getting populated correctly in the pre-test.  My problem is that when I pass that same array to the Test script, it fails with this error:
    "The SqlParameter is already contained by another SqlParameterCollection".

    I could clone the array, but that seems like a hack.  I can't seem to locate the SqlParameterCollection object that is using them, or else I'd just clear the collection before I pass the array. 

    My intent is to pass the same array of parameters to the pre-test, test, and post-test.  Any of those phases may set some, all or none of the parameters.  Any phase may use any of the values stored in the parameters.

    Any ideas, o wise, generous, and good-looking ones?
    MCTS:SQL2005
    Saturday, February 28, 2009 12:08 AM

Answers

  • This is an ADO.NET behavior we really do not have any influence over.
    Only workaround I know is to call Parameters.Clear() on the SqlCommand object.
    GertD @ www.DBProj.com
    • Proposed as answer by Gert Drapers (MSFT) Saturday, April 4, 2009 6:10 AM
    • Marked as answer by MKrebs Saturday, April 4, 2009 4:25 PM
    Saturday, April 4, 2009 6:10 AM

All replies

  • *bump*

    Seriously, anyone have an idea?  In the code for a database unit test, I want to create a SqlParameter object, and pass it to the pre-test, test, and post-test scripts so each one can consume it, change it, pass it back.  Can I get access to the SQLParameterCollection object so I can clear it?  Is there any other solution than cloning the SqlParameter objects?

    BTW, this is on VS 2008 DB Edition with SP1 (not GDR)
    MCTS:SQL2005
    Monday, March 9, 2009 2:55 PM
  • This is an ADO.NET behavior we really do not have any influence over.
    Only workaround I know is to call Parameters.Clear() on the SqlCommand object.
    GertD @ www.DBProj.com
    • Proposed as answer by Gert Drapers (MSFT) Saturday, April 4, 2009 6:10 AM
    • Marked as answer by MKrebs Saturday, April 4, 2009 4:25 PM
    Saturday, April 4, 2009 6:10 AM
  • That's what I was afraid of.  Thank you for your help.

    Is there anyway to access the SqlCommand object from the DatabaseUnitTestObject?  I've tried to get to it through the DatabaseContext object, but can't find anything.
    MCTS:SQL2005
    Saturday, April 4, 2009 4:29 PM
  • Sorry, today there is no way to access the DbCommand object instance that is used, unless there exists a way to get it of the ExecutionResult which contains a DataSet object, but I expect that to be disconnected by the time you get it.
    GertD @ www.DBProj.com
    Saturday, April 4, 2009 6:56 PM