3 Replies - 157 Views - Last Post: 07 January 2020 - 07:57 AM Rate Topic: -----

#1 fearfulsc2   User is offline

  • D.I.C Regular

Reputation: 18
  • View blog
  • Posts: 301
  • Joined: 25-May 16

Unit Test / Mock objects without DI or Interfaces

Posted 07 January 2020 - 07:09 AM

Hi everyone, I have only ever unit tested code that had either interfaces or used interfaces with DI.

I am trying to unit test a piece of code like the code below and am not sure of how I can write a unit test (not an integration test).

Has anyone worked with that before?

 public async Task<string> SomeFunction(int someNumber)
        {
            // replace STATEMENT with S.....Q.....
            using (STATEMENTConnection conn = new QUERYConnection(_dbConnectionString))
            {
                // OPEN CONNECTIOn
                await conn.();

                using (STATEMENTCommand cmd = conn.())
                {
                    cmd.= CommandType.;
                    cmd.= @"
                        // seems like I can't type in some query statements here otherwise I can't post
                    ";

                    cmd.Parameters.AddWithValue("@ThisNum", someNum);

                    using (DbDataReader reader = await cmd.())
                    {
                        if (reader.HasRows)
                            return "valid";
                        else
                            return "invalid";
                    }
                }
            }
        }


Replace STATEMENT with S.....Q......
I figured I could at least mock the class but then I run into other issues such as the STATEMENTConnection library that I am using and it's opening a connection to the db.

Any tips/recommendations?

Is This A Good Question/Topic? 0
  • +

Replies To: Unit Test / Mock objects without DI or Interfaces

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7244
  • View blog
  • Posts: 24,556
  • Joined: 05-May 12

Re: Unit Test / Mock objects without DI or Interfaces

Posted 07 January 2020 - 07:33 AM

Have you looked at Microsoft's Mocks and Fakes? It uses the linker to replace objects or intercept calls, instead of the compiler using interfaces and/or dependency injection.
Was This Post Helpful? 1
  • +
  • -

#3 fearfulsc2   User is offline

  • D.I.C Regular

Reputation: 18
  • View blog
  • Posts: 301
  • Joined: 25-May 16

Re: Unit Test / Mock objects without DI or Interfaces

Posted 07 January 2020 - 07:51 AM

I'll have to take a look into that. I know that there are things such as shims and fakes. Would I have to do that for each class that I want to unit test?
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7244
  • View blog
  • Posts: 24,556
  • Joined: 05-May 12

Re: Unit Test / Mock objects without DI or Interfaces

Posted 07 January 2020 - 07:57 AM

Thanks for the correction! Yes: shims and fakes.

I think you do it per assembly. Then it is up to you if you need to do something with a class or leave it alone.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1