0

I am using Mstest for the first time and writing a unit test. I used ClassInitiaze attribute for setup. Since I did that I have been getting this message

Message: Method TryDI.Tests.HouseKeeperServiceTests.TestSetup has wrong signature. The method must be static, public, does not return a value and should take a single parameter of type TestContext. Additionally, if you are using async-await in method then return-type must be Task.

What am I doing wrong here?

        [TestClass]
        public class HouseKeeperServiceTests
        {
            private HousekeeperService _service;
            private Mock<IStatementGenerator> _statementGenerator;
            private Mock<IEmailSender> _emailSender;
            private Mock<IXtraMessageBox> _messageBox;
            private DateTime _statementDate = new DateTime(2017, 1, 1);
            private Housekeeper _houseKeeper;
      
            [ClassInitialize]
            public void TestSetup()
            {    
                _houseKeeper = new Housekeeper { Email = "a", FullName = "b", Oid = 1, StatementEmailBody = "c" };

                var unitOfWork = new Mock<IUnitOfWork>();
                unitOfWork.Setup(uow => uow.Query<Housekeeper>())
                    .Returns(new List<Housekeeper>
                    {
                       _houseKeeper

                    }.AsQueryable());

                _statementGenerator = new Mock<IStatementGenerator>();
                _emailSender = new Mock<IEmailSender>();
                _messageBox = new Mock<IXtraMessageBox>();

                var service = new HousekeeperService(unitOfWork.Object,
                                                     _statementGenerator.Object,
                                                     _emailSender.Object,
                                                     _messageBox.Object);
            }

            [TestMethod]
            public void SendStatementEmails_WhenCalled_GenerateStatements()
            {
                _service.SendStatementEmails(_statementDate);

                _statementGenerator.Verify(sg =>
                   sg.SaveStatement(_houseKeeper.Oid, _houseKeeper.FullName, (_statementDate)));
            }
        }
        
        
Julian
  • 33,915
  • 22
  • 119
  • 174
Baba
  • 2,059
  • 8
  • 48
  • 81

2 Answers2

0

It's in the error message?

TryDI.Tests.HouseKeeperServiceTests.TestSetup has wrong signature. The method must be static, ..and should take a single parameter of type TestContext ...

Working example:

  [ClassInitialize()]
  public static void ClassInit(TestContext context)
  {
     MessageBox.Show("ClassInit");
  }

Another option is to use the [TestInitialize]

See also the docs

Julian
  • 33,915
  • 22
  • 119
  • 174
  • By the time I do that, I get a lot of squiggy red lines on the private variables I declared at the begining of the code. which I am using in the Testsetup method – Baba Apr 28 '21 at 00:34
  • I think it should be [TestInitialize]. This worked for me without any problem – Baba Apr 28 '21 at 00:37
0

You could use TestInitialize:

    [TestInitialize]
    public static void TestSetup() { }

but if you want to use ClassInitialize see this post:

error CLassInitialize has wrong signature. The method must be static, public, does not return a value

"The ClassInitialize attribute need to have paranthesis added and secondly try to have the method name different than attribute as it's probably confusing the compiler"

For example:

[ClassInitialize()]
public static void TestSetup(TestContext context){  }

Anyway you should know the differece between TestInitializer and ClassInitializer:

"TestInitialize runs before every test that is declared on the the same class where the attribute is declared."

"ClassInitialize runs only on the initialization of the class where the attribute is declared. In other words it won't run for every class. Just for the class that contains the ClassInitialize method."

For more info: https://stackoverflow.com/questions/22999816/testinitialize-vs-classinitialize#:~:text=TestInitialize%20runs%20before%20every%20test,where%20the%20attribute%20is%20declared.&text=If%20you%20want%20a%20method,classes'%20initialization%20use%20the%20AssemblyInitialize%20.