-3

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.

7
  • 2
    It is an assertion. It checks some expectations and throws an exception when they are not fulfilled. Commented Feb 23, 2018 at 14:01
  • 7
    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. Commented Feb 23, 2018 at 14:01
  • 2
    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. Commented Feb 23, 2018 at 14:01
  • 1
    Assertions typically throw an exception if untrue or continue with the next line without any return value. Commented Feb 23, 2018 at 14:06
  • Well the one thing I just now saw on the front page of 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. Commented Feb 23, 2018 at 14:07

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

but when something is null you will get a null pointer exception on accessing ti its properties or functions including Should()
@Navid_pdp11 Should() is an extension method. You're actually calling a static method that checks the object before it. See learn.microsoft.com/en-us/dotnet/csharp/programming-guide/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.