chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
+1 vote
As you may look at the follownig code, here is the scenario and my goal :
 
Goal  : 
  1.  Prevent ScratchFoot() method from being called, so we need to fake Godzilla so it deosn't call tickle()
  2.  Check if startMovie() was called.
 
 
 Notes : 
  1.  tickle is a virtual method and only exists in the Godzila class.
  2.  in our system under test we're calling MakeHimLaugh() method.
 
 
namespace JurassicEra
{

	public abstract class Godzilla
	{
		public Godzilla()
		{
			// some unrelated logic 
		}

		protected virtual byte[] tickle(object feather , object water)
		{
			if(feather == null)
			{
				return null;
			}
			return this.ScratchFoot(feather, water);
		}
	}
}

namespace InstagramEra
{
	public sealed class Lizard : Godzilla
	
	private static Lizard instance;
	private static object syncRoot = new object();
	
	public static Lizard getInstance()
	{
	    if (instance == null)
		{
			lock (syncRoot)
			{
				if (instance == null)
				{
					instance = new Lizard();
				}
			}
		}
		return instance;
	}
	
	private Lizard()
	{
	}
	
	public void MakeHimLaugh(object feather , object water)
	{
		Theater theater = new Comedy();
		object poorLizard = this.tickle(feather, water);
		
		theater.startMovie();		
	}	
}
 *** Please save yourself and me the time to argue that why an abstract class has public constructor and other design challenges. This is my current structure and I'm only here to test it.
 
 
 Secnarios :
 
  
 I tried all the following scenarios, and non of them worked for me, meaning in all 3 cases tickle() was called.
 
 what I did; I'm passing null for feather, so this method will return null so it won't get to call ScratchFoot method.
 
 
 
 
public void CheckIfMovieStarted()
{
	var fakeGodzilla = Isolate.Fake.AllInstances<Godzilla>();	
	Isolate.NonPublic.WhenCalled(fakeGodzilla, "tickle").WillReturn(null);	
}

public void CheckIfMovieStarted()
{
	Isolate.Invoke.StaticConstructor<Lizard>();
	var fakeLizard = Lizard.getInstance();	
	Isolate.NonPublic.WhenCalled(fakeLizard, "tickle").WillReturn(null);	
}

public void CheckIfMovieStarted()
{
	var fakeLizard = Lizard.getInstance();	
	Isolate.NonPublic.WhenCalled(fakeLizard, "tickle").WillReturn(null);	
}

 

Thank you 
 
 
 
 
 
 
 
 
 
 
 
asked by aringan (1.1k points)

Please log in or register to answer this question.

...