public int Add(Message message)
 {
     var errors = ValidationRunner.Run(message);
     if (errors != null && errors.Count > 0)
     {
         throw new ValidationException(errors);
     }
     return Repository.Add(message);
 }
 public bool Save(Message message)
 {
     try
     {
         var original = Messages.Single(m => m.Id == message.Id);
         var index = Messages.IndexOf(original);
         message.User = original.User;
         Messages[index] = message;
         return true;
     }
     catch (Exception)
     {
         return false;
     }
 }
        public void Add_Throws_Exception_If_Validation_Returns_Errors()
        {
            var invalidMessage = new Message();

            //mock a failed validation
            var mockValidationRunner = new Mock<IValidationRunner>();
            mockValidationRunner.Expect(v => v.Run(invalidMessage))
                .Returns(new List<ValidationError>
                             {
                                 new ValidationError("prop",
                                                     "error")
                             });

            service = new InMemoryMessageService(null, mockValidationRunner.Object);
            Assert.Throws<ValidationException>(() => service.Add(invalidMessage));

            mockValidationRunner.VerifyAll();
        }
        public void getting_edit_calls_msgservice_to_get_message_and_pass_to_viewdata()
        {
            //arrange
            var id = 1;
            var name = "Mr. Burn's Message";
            var message = new Message{ Id = id, Name = name };
            var svc = new Mock<IMessageService>();
            svc
                .Expect( s => s.GetById( It.Is<int>( i => i == id ) ) )
                .Returns( message );
            controller.MessageService = svc.Object;

            //act
            var result = controller.Edit( id );

            //assert
            Assert.AreSame( message, result.ViewData.Model );
        }
 public void SetUp()
 {
     message = new Message()
     {
         Subject = "My newsletter subject",
         Name = "October newsletter",
         Text = "Hello subscriber",
         Html = "Hello <b>subscriber</b>"
     };
 }
        public void SetUp()
        {
            MockMembership = new Mock<MembershipProvider>();
            MockMembershipUser = new Mock<MembershipUser>();
            user = new Mock<IPrincipal>();
            identity = new Mock<IIdentity>();
            httpContext = new Mock<HttpContextBase>();
            request = new Mock<HttpRequestBase>();
            controllerbase = new Mock<ControllerBase>();

            SetupMocks(Username, UserId);

            controller = GetController();

            model = new Message()
                        {
                            Subject = "My newsletter subject",
                            Name = "October newsletter",
                            Text = "Hello subscriber",
                            Html = "Hello <b>subscriber</b>"
                        };
        }
        public void edit_message_should_save_changes_to_repository()
        {
            PopulateRepository();
            var repo = (IMessageRepository)kernel.Get(typeof(IMessageRepository));
            var oldMessage = repo.Get().SingleOrDefault(m => m.Id == 1);
            var oldName = oldMessage.Name;
            var newName = "New name";
            var newMessage = new Message
                                 {
                                     Name = newName,
                                     Html = oldMessage.Html,
                                     Id = oldMessage.Id,
                                     Subject = oldMessage.Subject,
                                     Text = oldMessage.Text,
                                     User = oldMessage.User
                                 };
            Assert.AreEqual(oldName, repo.Get().SingleOrDefault(m => m.Id == 1).Name);

            var result = controller.Edit(newMessage);

            Assert.AreEqual(newName, repo.Get().SingleOrDefault(m => m.Id == 1).Name);
        }
 public bool Save(string username, Message message)
 {
     return Repository.Save(message);
 }
 public void SetUp()
 {
     vrunner = new ValidationRunner();
     service = new InMemoryMessageService(null, vrunner);
     message = new Message()
                   {
                       Html = "<b>Hello user</b>",
                       Name = "Message Name",
                       Subject = "Subject",
                       Text = "Hello user"
                   };
 }
 public int Add(Message message)
 {
     message.Id = AutoId;
     Messages.Add(message);
     return message.Id;
 }
        public void posting_to_edit_gets_message_and_updates_with_input()
        {
            //arrange
            var id = 1;
            var name = "Not Mister Burns";
            var subject = "Welcome to Springfield!";
            var text = "Lorem ipsum dolor sit amet.";
            var html = "<p>Lorem <b>ipsum</b> dolor sit amet.</p>";
            var form = new FormCollection{
                                         	{ "Name", name },
                                         	{ "Subject", subject },
                                         	{ "Text", text },
                                         	{ "Html", html }
                                         };
            controller.ValueProvider = form.ToValueProvider();
            var message = new Message{ Id = id, Name = "Mister Burns" };
            var svc = new Mock<IMessageService>();
            svc
                .Expect( s => s.GetById( It.Is<int>( i => i == id ) ) )
                .Returns( message );
            svc
                .Expect( s => s.Update( It.Is<Message>( m =>
                                                        m.Id == id &&
                                                        m.Name == name &&
                                                        m.Subject == subject &&
                                                        m.Text == text
                                            ) ) ).Returns( true );
            controller.MessageService = svc.Object;

            //act
            controller.Edit_Post( id );

            //assert
            svc.VerifyAll();
        }
        public void posting_to_create_creates_and_saves_a_new_message_object()
        {
            //arrange
            var name = "Mister Burns";
            var subject = "Some Subject";
            var text = "Some Text";
            var message = new Message{ Name = name };
            var svc = new Mock<IMessageService>();
            svc.Expect( s => s.Add( It.IsAny<Message>() ) ).Returns( 1 );
            controller.MessageService = svc.Object;
            var form = new FormCollection{ { "Name", name }, { "Subject", subject }, { "Text", text } };
            controller.ValueProvider = form.ToValueProvider();

            //act
            var result = controller.Create_Post();

            //assert
            svc.VerifyAll();
        }