-4

I have read through fluentassertions.com and still confused as to what happens if a var is null or not null.

so for instance if you have

object someitem = null;

then have

someitem.Should().NotBeNull(); 

what happens if it is null? this example is pretty much the same code I am reviewing of another coder. to me it seems like Should().NotBeNull() should return a boolean value but I am not seeing that in any documentation I have reviewed. If it does simply just return a boolean value I don't understand why the code someitem.Should().NotBeNull(); would be all by itself.

I know some may say well run the code and set a break point to see what it is doing. unfortunately I cannot run the code from my box due to not having a dev db setup and don't have direct access to live db. So running the code does nothing for me.

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
scripter78
  • 1,117
  • 3
  • 22
  • 50
  • 1
    It is an [assertion](https://en.wikipedia.org/wiki/Test_assertion). It checks some expectations and throws an exception when they are not fulfilled. – molnarm Feb 23 '18 at 14:01
  • 5
    Why do you need some dev db setup to see how it works? just do `string s = null; s.Should().BeNotNull()` and see what happens. – Evk Feb 23 '18 at 14:01
  • 1
    Whether you don't have access to a DB or not is completely unrelated to your question. You can test the behavior of `Should().NotBeNull()` without a database. – mason Feb 23 '18 at 14:01
  • 1
    Assertions typically throw an exception if untrue or continue with the next line without any return value. – eckes Feb 23 '18 at 14:06
  • Well the one thing I just now saw on the front page of http://fluentassertions.com which I did not see in the documentation area is that fluent assertions is only for unit testing. I am not familiar with unit testing and not sure why that would still be included in live code. – scripter78 Feb 23 '18 at 14:07
  • 2
    You would need to ask the person who *wrote the live code*. Not us. We didn't write it. – mason Feb 23 '18 at 14:09
  • Well now that I understand that Assertions are for testing purposes I was able to perform a clearer search. This is what I came up with. Although this post helped me I can understand that this may not be the clearest post for others. that all being said as for my follow up question https://stackoverflow.com/questions/17732/when-should-assertions-stay-in-production-code – scripter78 Feb 23 '18 at 14:14
  • as an additional followup for why you would keep them in live code I think this article explained it very well. http://wiki.c2.com/?ShipWithAssertionsOn – scripter78 Feb 23 '18 at 14:18

1 Answers1

1

If someItem is null an exception will be thrown, otherwise it will pass the assertion and the test will succeed.

You can see how the exception is thrown on each testing framework on the Execution folder https://github.com/fluentassertions/fluentassertions/tree/master/Src/FluentAssertions/Execution

P.N.
  • 645
  • 6
  • 18