internal IExpectMatchSyntax <TResult> Parse <TResult>(Func <T, TResult> function)
        {
            MethodInfo method;

            object[] args;
            using (var recorder = new InvocationRecorder())
            {
                try
                {
                    function(mock);
                }
// ReSharper disable EmptyGeneralCatchClause
                catch { }
// ReSharper restore EmptyGeneralCatchClause
                method = recorder.Invocation.Method;
                args   = recorder.Invocation.Arguments;
            }

            if (method.IsProperty())
            {
                SetupPropertyExpectation(method, args);
            }
            else             //is a method
            {
                SetupMethodExpectation(method, args);
            }

            return(new ExpectationSyntax <T, TResult>(this));
        }
示例#2
0
        public void BeforeScenario()
        {
            _publishedDirectory = new DirectoryInfo(Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()));

            var invocationRecorder = new InvocationRecorder();
            var generator          = new ProxyGenerator();

            var eventBus      = new EventBus();
            var commandBus    = new CommandBus(eventBus);
            var gitRepository = Dummy.GitRepository();
            var slugFactory   = Dummy.SlugFactory();
            var yamlMatter    = Dummy.YamlMatter();

            var blogPostRepository = generator.CreateInterfaceProxyWithTarget <IBlogPostRepository>(new BlogPostRepository(new BlogPostRepositorySettings(_publishedDirectory, "blog"), gitRepository, yamlMatter), invocationRecorder);
            var pageRepository     = generator.CreateInterfaceProxyWithTarget <IPageRepository>(new PageRepository(new PageRepositorySettings(_publishedDirectory), new BlogPostRepositorySettings(_publishedDirectory, "blog"), gitRepository, yamlMatter), invocationRecorder);
            var fileRepository     = generator.CreateInterfaceProxyWithTarget <IFileRepository>(new FileRepository(new FileRepositorySettings(_publishedDirectory), gitRepository), invocationRecorder);

            Configuration.Configure(commandBus, blogPostRepository, pageRepository, fileRepository);

            // Register Instances
            _objectContainer.RegisterInstanceAs(commandBus, typeof(ICommandBus));
            _objectContainer.RegisterInstanceAs(eventBus, typeof(IEventBus));
            _objectContainer.RegisterInstanceAs(gitRepository, typeof(IGitRepository));
            _objectContainer.RegisterInstanceAs(blogPostRepository, typeof(IBlogPostRepository));
            _objectContainer.RegisterInstanceAs(pageRepository, typeof(IPageRepository));
            _objectContainer.RegisterInstanceAs(slugFactory, typeof(ISlugFactory));
            _objectContainer.RegisterInstanceAs(invocationRecorder, typeof(InvocationRecorder));
        }
        private IMatchSyntax GetEventExpectation(Action <T> action, out string eventName)
        {
            object[] arguments;
            using (var recorder = new InvocationRecorder())
            {
                action(mock);
                eventName = recorder.Invocation.Method.Name;
                arguments = recorder.Invocation.Arguments;
            }

            var symbol        = eventName.StartsWith(Constants.ADD) ? " += " : " -= ";
            var methodMatcher = new MethodNameMatcher(eventName, MockObject.MockedTypes.PrimaryType);

            var descriptionOverride = new DescriptionOverride(eventName.Replace(Constants.ADD, string.Empty).Replace(Constants.REMOVE, string.Empty) + symbol, methodMatcher);

            EnsureMatchingMethodExistsOnMock(methodMatcher, "an event named " + eventName);

            BuildableExpectation.MethodMatcher = descriptionOverride;

            if (arguments.Length == 1 && arguments[0] != null && !((MulticastDelegate)arguments[0]).Method.Name.Contains(">"))
            {
                WithArguments(new DelegateMatcher((MulticastDelegate)arguments[0]));
            }
            else
            {
                WithArguments(Is.Anything);
            }
            return(this);
        }
示例#4
0
        public UploadFileSteps(GivenData given, ActualData actual, ICommandBus commandBus, IEventBus eventBus, InvocationRecorder invocationRecorder)
        {
            _given              = given;
            _actual             = actual;
            _commandBus         = commandBus;
            _invocationRecorder = invocationRecorder;

            eventBus.SubscribeTo <UploadedFileEvent>(HandleUploadedFileEvent);
        }
示例#5
0
 public PublishAsyncSteps(GivenData given, ActualData actual, ScenarioHelpers scenarioHelpers, InvocationRecorder invocationRecorder, ISlugFactory slugFactory)
 {
     _given              = given;
     _actual             = actual;
     _blogPostRepository = scenarioHelpers.BlogPostRepository;
     _blogPostRepositoryWorkingDirectory = scenarioHelpers.BlogPostRepositoryWorkingDirectory;
     _invocationRecorder = invocationRecorder;
     _slugFactory        = slugFactory;
 }
示例#6
0
        public PublishBlogPostSteps(GivenData given, ActualData actual, ICommandBus commandBus, IEventBus eventBus, InvocationRecorder invocationRecorder)
        {
            _given              = given;
            _actual             = actual;
            _commandBus         = commandBus;
            _invocationRecorder = invocationRecorder;

            _given.Published = DateTime.Now;

            eventBus.SubscribeTo <PublishedBlogPost>(HandleCreatedBlogPostEvent);
        }
示例#7
0
        // ReSharper disable once SuggestBaseTypeForParameter
        private IGitRepository GitRepositoryFactory(InvocationRecorder invocationRecorder)
        {
            var workingDirectory = InitGitRepository();

            var generator     = new ProxyGenerator();
            var gitRepository = generator.CreateInterfaceProxyWithTarget <IGitRepository>(new GitRepository(new GitRepositorySettings(workingDirectory, "dummy", "*****@*****.**")), invocationRecorder);

            BlogPostRepositoryWorkingDirectory = workingDirectory;

            return(gitRepository);
        }
        public IActionSyntax SetPropertyTo(Action <T> action)
        {
            MethodInfo method;

            object[] args;
            using (InvocationRecorder recorder = new InvocationRecorder())
            {
                try
                {
                    action(mock);
                }
// ReSharper disable EmptyGeneralCatchClause
                catch { }
// ReSharper restore EmptyGeneralCatchClause
                method = recorder.Invocation.Method;
                args   = recorder.Invocation.Arguments;
            }

            if (!method.IsProperty())
            {
                throw new InvalidOperationException("This method expects a property, not a method, or event.");
            }

            if (method.IsSpecialName && method.Name == Constants.SET_ITEM)
            {
                //properties have args when used as indexer like SetPropertyTo(m=>m.prop[1] = "d")
                IValueSyntax valueSyntax = SetIndexer(args.Take(args.Length - 1).ToArray());
                valueSyntax.To(Is.EqualTo(args.Last()));
            }
            else if (method.IsSpecialName && method.Name == Constants.GET_ITEM)
            {
                throw new InvalidOperationException("Using a property as a getter in the SetPropertyTo method is not supported.");
            }
            else
            {
                IValueSyntax valueSyntax = SetProperty(method.Name.Substring(Constants.SET.Length));
                valueSyntax.To(Is.EqualTo(args[0]));
            }

            return(this);
        }
        public IAutoValueSyntax <TProperty> SetProperty <TProperty>(Func <T, TProperty> function)
        {
            MethodInfo method;

            object[] args;
            using (var recorder = new InvocationRecorder())
            {
                try
                {
                    function(mock);
                }
// ReSharper disable EmptyGeneralCatchClause
                catch { }
// ReSharper restore EmptyGeneralCatchClause
                method = recorder.Invocation.Method;
                args   = recorder.Invocation.Arguments;
            }

            if (!method.IsProperty())
            {
                throw new InvalidOperationException("This method expects a property, not a method, or event.");
            }

            IValueSyntaxBuilder valueSyntax;

            if (method.IsIndexer())
            {
                //properties have args when used like SetProperty(m=>m.prop[1] = "d") or SetProperty(m=>m.prop[1]) or SetProperty(m=>m.prop[1,3])
                valueSyntax = method.Name == Constants.GET_ITEM ? SetIndexer(args) : SetIndexer(args.Take(args.Length - 1).ToArray());
            }
            else
            {
                var name = method.Name.Replace(Constants.SET, string.Empty).Replace(Constants.GET, string.Empty);                 //get is needed if is used like SetProperty(m=>m.prop)
                valueSyntax = SetProperty(name);
            }

            return(new PropertyValueBuilder <TProperty>(valueSyntax));
        }
        internal IExpectMatchSyntax Parse(Action <T> action)
        {
            MethodInfo method;

            object[] args;
            using (var recorder = new InvocationRecorder())
            {
                try
                {
                    action(mock);
                }
// ReSharper disable EmptyGeneralCatchClause
                catch { }
// ReSharper restore EmptyGeneralCatchClause
                method = recorder.Invocation.Method;
                args   = recorder.Invocation.Arguments;
            }

            if (method.IsEvent())
            {
                string eventName;
                GetEventExpectation(action, out eventName);

                return(new ExpectationSyntax <T>(this, eventName));
            }
            else if (method.IsProperty())
            {
                SetupPropertyExpectation(method, args);
            }
            else
            {
                SetupMethodExpectation(method, args);
            }

            return(new ExpectationSyntax <T>(this));
        }