locked
Checking value changed in Action (unit test) RRS feed

  • Question

  • I have the following code snippet:

    [Test] public void ValidCancel_ReturnsSuccess() {

    // TODO: Rewrite this action to really check the cancel bool cancel; var message = new MessageHubCancellableGenericMessage<string>(this, "Test", () => cancel = true ); Assert.IsNotNull(message.Sender); Assert.IsNotNull(message.Content); }

    I'm passing an Action as an argument where I'm setting the variable cancel as true, but I need to check if the value was changed to true (an Assert.IsTrue(cancel)), but when I do that the value is false. Is it possible to check the change of the variable cancel?


    Edit: Here is the code of the class that I want to test.-

    using System;
    
    public abstract class MessageHubMessageBase : IMessageHubMessage
    {
        private readonly WeakReference _sender;
    
        protected MessageHubMessageBase(object sender)
        {
            if (sender == null)
                throw new ArgumentNullException(nameof(sender));
    
            _sender = new WeakReference(sender);
        }
    
        public object Sender => _sender?.Target;
    }
    
    public class MessageHubGenericMessage<TContent>
        : MessageHubMessageBase
    {
        public MessageHubGenericMessage(object sender, TContent content)
            : base(sender)
        {
            Content = content;
        }
    
        public TContent Content { get; protected set; }
    }
    
    public class MessageHubCancellableGenericMessage<TContent> 
        : MessageHubMessageBase
    {
        public MessageHubCancellableGenericMessage(object sender, TContent content, Action cancelAction)
            : base(sender)
        {
            Content = content;
            Cancel = cancelAction ?? throw new ArgumentNullException(nameof(cancelAction));
        }
    
        public Action Cancel { get; protected set; }
    
        public TContent Content { get; protected set; }
    }


    Wednesday, November 29, 2017 11:58 PM

All replies

  • Hi ArCiGo,

    What type of program you want to unit test? Wpf, Winform or others. And your question is more related to unit test, I will move the thread to Visual Studio Unit Testing forum for suitable support.

    If you have some grammar or code errors in using c#, please feel free to contact us. We will try our best to give you a solution.

    Best Regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Edited by Fei Hu Thursday, November 30, 2017 6:07 AM
    Thursday, November 30, 2017 6:02 AM
  • If your question is just to confirm that your code would actually work: Yes (search for "variable scope in lambda expressions" there).

    Otherwise, I don't get the question. The variables scope is definitely the scope of the TestMethod, not the lambda expression. Since you created it in the TestMethod not in the lambda expression.

    Of course to actually test the cancel variable you would need a Assert.IsTrue(cancel) in there.

    We don't know what the Properties of your object do (message.Sender, message.Content) so if it has to do with those variables and Content returns the cancel, you should present that code. Simple answer still is, yes, if Content returns cancel it will be returned as true.

    As long as the lambda actually gets executed at some point, which we cant know cause we dont know your MessageHubCancellableGenericMessage class.


    Please be so kind to close your Threads when you found an answer, these Threads should help everyone with similar issues.
    You can close a Thread via the"Mark as Answer" link below posts. You can mark your own posts as answers if you were not helped out but found a solution, in such a case, please provide the answer.
    Happy coding
    PS: I assure everyone that I did not ever had the desire to offend anyone.



    • Edited by MDeero Thursday, November 30, 2017 10:12 AM
    Thursday, November 30, 2017 10:08 AM
  • Hi!

    I posted the code that I want to test.

    Thursday, November 30, 2017 3:48 PM
  • It's more related to class library
    Thursday, November 30, 2017 10:44 PM
  • bool cancel= false;
                var message = new MessageHubCancellableGenericMessage<string>(this, "Test", () => cancel = true);
                message.Cancel();
    
                Assert.IsTrue(cancel); 

    try the code above in your test method, which works as expected in my side.

     

    Regards,

    Fletcher


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • Edited by Fletch Zhou Friday, December 1, 2017 7:52 AM
    • Proposed as answer by Fletch Zhou Monday, December 4, 2017 6:58 AM
    Friday, December 1, 2017 7:52 AM