We are excited to announce new JustMock internal build release published today. It's our second internal build after 2011 Q1 release. In the past weeks the product team has been focused mainly on polishing JustMock but hasn’t forgotten to add new stuff as well. Here is a comprehensive list of the functionalities that have been added and improved in the latest internal build including thorough examples:

What’s new:

  • New : Deep LINQ mocking support (mocking join query):
    Example (Details can be found in the LinqFixture.cs under the supplied examples):
            var simple = new SimpleData();

            Mock.Arrange(() => simple.Products).ReturnsCollection(GetFakeProducts());
            Mock.Arrange(() => simple.Categories).ReturnsCollection(GetCategories());

            var query = from product in simple.Products
                        join category in simple.Categories on product.CategoryId equals category.Id
                        select category.Id;

            Assert.Equal(2, query.Count());

 

  • Support for asserting specific mock call using the fluent interface under Telerik.JustMock.Helpers.
    Example:
       IFileReader fileReader = Mock.Create<IFileReader>();
       const string expected = @"c:\JustMock";
      
       fileReader.Arrange(x => x.Path).Returns(expected).OccursOnce();
      
       Assert.Equal(expected, fileReader.Path);
       fileReader.Assert(x => x.Path);


What’s fixed:

  • Fix : Issue with mocking non-public member when type is proxied.
  • Fix : Exception for uncommitted class during Mock.Create that has custom GetHashCode method and throws exception when environment is not properly set.
  • Fix : Ability to set expectations for properties that are declared in the test class.
    Example :
        [TestMethod]
        public void ShouldBeAbleToSetExpectationsForPropertyFromTestClass()
        {
            this.Foo = Mock.Create<IFoo>();
            Mock.Arrange(() => this.Foo.Echo(1)).Returns((int x) => x);
            Assert.Equal(1, this.Foo.Echo(1));
        }

        public interface IFoo
        {
        [TestMethod]
        public void ShouldBeAbleToSetExpectationsForPropertyFromTestClass()
        {
            this.Foo = Mock.Create<IFoo>();
            Mock.Arrange(() => this.Foo.Echo(1)).Returns((int x) => x);
            Assert.Equal(1, this.Foo.Echo(1));
        }

        public interface IFoo
        {
            int Echo(int arg);
        }

        public IFoo Foo { get; set; }

  • Fix : Automatically set call original flag on framework methods If mocked in a different test but not in the current.
  • Fix : Issue with generic constraint used for base class.
    Example:
        public abstract class BaseProduct<T> where T : Product
        {
            public T Clone()
            {
                return default(T);
            }
        }

        public class ExtendedProduct : BaseProduct<Product>
        {

        }

        [TestMethod]
        public void ShouldBeAbleToMockMemberFromNonGenericTypeInheritingConstraintBaseType()
        {
            var businessObject = Mock.Create<ExtendedProduct>();

            Mock.Arrange(() => businessObject.Clone()).MustBeCalled();

            businessObject.Clone();

            Mock.Assert(businessObject);
        }

  • Fix : Ability to apply Constructor.Mocked on Sealed class
    Example:
        public sealed class FooNonDefault
        {
            public FooNonDefault(int arg1)
            {
                throw new ArgumentException("Must provide a valid argument");
            }
        }
      
 
        [TestMethod]
        public void ShouldExecuteOriginalConstructorIfMockFlagIsNotSpecified()
        {
            Assert.Throws<ArgumentException>(() => Mock.Create<FooNonDefault>(10));
        }

  • Fix : Exception when params object[] is set with value type argument in Mock.Arrange.
    Example:
        public class Foo<TEntity>
        {
            public virtual TEntity GetByKey(params object[] keyValues)
            {
                return default(TEntity);
            }
        }


        [TestMethod]
        public void ShouldAssetSetupWithValueTypeArgumentPassForParamsObjectArray()
        {
            var foo = Mock.Create<Foo<Product>>();
  
            const int expected = 1;
  
            Mock.Arrange(() => foo.GetByKey(expected)).Returns(() => null).MustBeCalled();
  
            foo.GetByKey(expected);
  
            Mock.Assert(foo);
        }

  • Fix : Mocking write only properly.
    Example:
        [TestMethod]
        public void ShoudlAssertSetOnlyProperty()
        {
            var foo = Mock.Create<IFoo>();

            foo.Track = true;

            Mock.AssertSet(() => foo.Track = true);
        }
 
        public interface IFoo
        {
                bool Track {set;}
        }

  • Fix : Mock.Raise exception for events defined inside the nested class (where the mocking type is injected).
    Example:
        private ProjectNavigatorViewModel viewModel;
        private ISolutionService solutionService;

        [TestInitialize]
        public void Initialize()
        {
            this.solutionService = Mock.Create<ISolutionService>();
            this.viewModel = new ProjectNavigatorViewModel(this.solutionService);
        }
 
        [TestMethod]
        public void ShouldAssertMockRaiseFromInsideAContainer()
        {
            var foo = Mock.Create<IFoo>();
            var projectEventArgs = new ProjectEventArgs(foo);
            Mock.Raise(() => this.solutionService.ProjectAdded += null, projectEventArgs);
            Assert.True(this.viewModel.IsProjectAddedCalled);
        }
 
        public class ProjectNavigatorViewModel
        {
            public ProjectNavigatorViewModel(ISolutionService solutionService)
            {
                this.SolutionService = solutionService;
                SolutionService.ProjectAdded += new EventHandler<ProjectEventArgs>(SolutionService_ProjectAdded);
            }

            void SolutionService_ProjectAdded(object sender, ProjectEventArgs e)
            {
                IsProjectAddedCalled = true;
            }

            public ISolutionService SolutionService { get; set; }
            public bool IsProjectAddedCalled { get; set; }
        }

        public class ProjectEventArgs : EventArgs
        {
            private IFoo foo;

            public ProjectEventArgs(IFoo foo)
            {
                this.foo = foo;
            }
        }

        public interface ISolutionService
        {
            event EventHandler<ProjectEventArgs> ProjectAdded;
        }

        public delegate void CustomEvent(string value);
        public delegate void EchoEvent(bool echoed);

And that sums it up. Stay tuned for the next build which will have several new features. Meanwhile you can always contact us through the support center and our forums.

Cheers and happy releasing!

The JustMock team
About the Author

Chris Eargle

is a Microsoft C# MVP with over a decade of experience designing and developing enterprise applications, and he runs the local .NET User Group: the Columbia Enterprise Developers Guild. He is a frequent guest of conferences and community events promoting best practices and new technologies. Chris is a native Carolinian; his family settled the Dutch Form region of South Carolina in 1752. He currently resides in Columbia with his wife, Binyue, his dog, Laika, and his three cats: Meeko, Tigger, and Sookie. Amazingly, they all get along... except for Meeko, who is by no means meek.

Comments

Comments are disabled in preview mode.