Colin
Colin

Reputation: 174

Nunit test fails when not in debug mode

when I run the TEST in debug mode it works, otherwise it fails.

I am using Moq.

When the following line is added everything works fine.

_repositoryMock.Setup(m => m.GetSingleBy(It.IsAny<Expression<Func<Configuration, bool>>>())).Returns((Configuration)null);

Without the line above it works only in DEBUG mode.

[Test]
public void ChangeServiceTier() {
 //Arrange
 const int configurationId = 6;
 const int serviceTierId = 12;

 //Act
 var result = _configurationService.ChangeServiceTier(configurationId, serviceTierId);

 //Assert
 result.Should().BeNull();

}

Method

 public ConfigurationDto ChangeServiceTier(int id, int serviceTierId) {
  var cfg = _repository.GetSingleBy < Configuration > (s => s.Id == id);

  if (cfg == null) {
   return null;
  }
  return _mapper.Map < ConfigurationDto > (cfg);
 }

TEST Fail rason: Message: Expected result to be <null>, but found ConfigurationDto...

For some reason, cfg is null only in debug MODE.

Any thoughts?

Upvotes: 0

Views: 1000

Answers (1)

Colin
Colin

Reputation: 174

Thanks @LordWilmore

It is unusual to have a constructor in a test class, and using SetUp as you suggested is the correct way. The difference will be that when you had a constructor, that constructor was called once, so if you didn't explicitly tidy up after each test then the affects of any previous test will be left lying around. The [SetUp] method is called before every [Test] method, so initialising in here means that you are creating a new instance each time. I would still suggest creating the item within each test if required, rather than as a class member, for this exact reason. – LordWilmore

Upvotes: 1

Related Questions