コード例 #1
0
        public async Task ResetColor_WithDefaultState_DoesNotClearState()
        {
            // Arrange
            var state = new ColorGrainState();

            var mockState = new Mock <IPersistentState <ColorGrainState> >();

            mockState.SetupGet(o => o.State).Returns(state);
            mockState.Setup(o => o.ClearStateAsync());

            var mockMapper = new Mock <IAttributeToFactoryMapper <PersistentStateAttribute> >();

            mockMapper.Setup(o => o.GetFactory(It.IsAny <ParameterInfo>(), It.IsAny <PersistentStateAttribute>())).Returns(context => mockState.Object);

            Silo.AddService(mockMapper.Object);

            var grain = await Silo.CreateGrainAsync <ColorGrain>(GrainId);

            // Act
            await grain.ResetColor();

            // Assert
            state.Color.Should().Be(Color.Unknown);
            state.Id.Should().Be(Guid.Empty);

            // Note that the following assert ties this test to the _implementation_ details. Generally, one should try
            // to avoid tying the test to the implementation details. It can lead to more brittle tests. However, one may
            // choose to accept this as a trade-off when the implementation detail represents an important behavior.
            mockState.Verify(o => o.ClearStateAsync(), Times.Never);
        }
コード例 #2
0
        public async Task GetColor_WithState_ReturnsColor()
        {
            // Arrange
            var state = new ColorGrainState
            {
                Color = Color.Red,
                Id    = GrainId,
            };

            var mockState = new Mock <IPersistentState <ColorGrainState> >();

            mockState.SetupGet(o => o.State).Returns(state);

            var mockMapper = new Mock <IAttributeToFactoryMapper <PersistentStateAttribute> >();

            mockMapper.Setup(o => o.GetFactory(It.IsAny <ParameterInfo>(), It.IsAny <PersistentStateAttribute>())).Returns(context => mockState.Object);

            Silo.AddService(mockMapper.Object);

            var grain = await Silo.CreateGrainAsync <ColorGrain>(GrainId);

            // Act
            var color = await grain.GetColor();

            // Assert
            color.Should().Be(Color.Red);
        }
コード例 #3
0
        public async Task SetColor_WithDefaultState_SetsState()
        {
            // Arrange
            var state = new ColorGrainState();

            var mockState = new Mock <IPersistentState <ColorGrainState> >();

            mockState.SetupGet(o => o.State).Returns(state);
            mockState.Setup(o => o.WriteStateAsync());

            var mockMapper = new Mock <IAttributeToFactoryMapper <PersistentStateAttribute> >();

            mockMapper.Setup(o => o.GetFactory(It.IsAny <ParameterInfo>(), It.IsAny <PersistentStateAttribute>())).Returns(context => mockState.Object);

            Silo.AddService(mockMapper.Object);

            var grain = await Silo.CreateGrainAsync <ColorGrain>(GrainId);

            // Act
            await grain.SetColor(Color.Blue);

            // Assert
            state.Color.Should().Be(Color.Blue);
            state.Id.Should().Be(GrainId);
        }
コード例 #4
0
        public void Init()
        {
            var clientFactory = new Mock <IHttpClientFactory>();
            var httpClient    = CreateMockedHttpClient(HttpStatusCode.OK, HttpResultValue);

            clientFactory.Setup(s => s.CreateClient(It.IsAny <string>()))
            .Returns(httpClient);

            Silo.AddService(clientFactory.Object);
        }
コード例 #5
0
        protected PlatformexTestKit()
        {
            var loggerFactory = new LoggerFactory();

            Silo.AddService <ILoggerFactory>(loggerFactory);

            Platform = new TestPlatform();
            Builder  = new PlatformBuilder(Platform);
            Silo.AddService <IPlatform>(Platform);
        }
コード例 #6
0
        public PersistantStreamNotWithinGrainStateTests()
        {
            _stateWithoutHandle = new PersistentListenerStateWithoutHandle();

            _persistentState = new Mock <IPersistentState <PersistentListenerStateWithoutHandle> >();
            _persistentState.SetupGet(o => o.State).Returns(_stateWithoutHandle);

            var mockMapper = new Mock <IAttributeToFactoryMapper <PersistentStateAttribute> >();

            mockMapper.Setup(o =>
                             o.GetFactory(It.IsAny <ParameterInfo>(), It.IsAny <PersistentStateAttribute>()))
            .Returns(context => _persistentState.Object);

            Silo.AddService(mockMapper.Object);

            _stream = Silo.AddStreamProbe <ChatMessage>(Guid.Empty, null, "Default");
        }
コード例 #7
0
        public async Task SetColor_WithMutatedState_ThrowsInconsistentStateException()
        {
            // Arrange
            var state = new ColorGrainState();

            var mockState = new Mock <IPersistentState <ColorGrainState> >();

            mockState.SetupGet(o => o.State).Returns(state);
            mockState.Setup(o => o.WriteStateAsync()).Throws <InconsistentStateException>();

            var mockMapper = new Mock <IAttributeToFactoryMapper <PersistentStateAttribute> >();

            mockMapper.Setup(o => o.GetFactory(It.IsAny <ParameterInfo>(), It.IsAny <PersistentStateAttribute>())).Returns(context => mockState.Object);

            Silo.AddService(mockMapper.Object);

            var grain = await Silo.CreateGrainAsync <ColorGrain>(GrainId);

            Action action = () => grain.SetColor(Color.Green);

            // Act+Assert
            action.Should().Throw <InconsistentStateException>();
        }
コード例 #8
0
        public async Task SayHelloTestShouldPrintDateWhenServiceProvided()
        {
            // Arrange
            const string greeting = "Bonjour";
            var          date     = DateTime.UtcNow.Date;

            var dateServiceMock = new Mock <IDateTimeService>();

            dateServiceMock.Setup(i => i.GetCurrentDate())
            .ReturnsAsync(() => date);

            Silo.AddService(dateServiceMock.Object);

            var grain = Silo.CreateGrain <HelloGrainWithServiceDependency>(10);

            // Act
            var reply = await grain.SayHello(greeting);

            // Assert
            Assert.NotNull(reply);
            Assert.Equal($"[{date}]: You said: '{greeting}', I say: Hello!", reply);

            dateServiceMock.Verify(i => i.GetCurrentDate(), Times.Once);
        }
コード例 #9
0
ファイル: UnitTest1.cs プロジェクト: platformex/platformex
 public UnitTest1()
 {
     Silo.AddService <IMemoState>(new MemoState(new InMemoryDbProvider <MemoModel>()));
 }
コード例 #10
0
        public UserRepositoryGrainTests()
        {
            _unitOfWork = new Mock <IUnitOfWork>();

            Silo.AddService(_unitOfWork.Object);
        }
コード例 #11
0
        public BlogPostGrainTests()
        {
            _unitOfWork = new Mock <IUnitOfWork>();

            Silo.AddService(_unitOfWork.Object);
        }
コード例 #12
0
 public AggregateTests()
 {
     Silo.AddService <ITestAggregateState>(new TestAggregateState());
 }