本文整理汇总了C#中AutoMoqer.GetMock方法的典型用法代码示例。如果您正苦于以下问题:C# AutoMoqer.GetMock方法的具体用法?C# AutoMoqer.GetMock怎么用?C# AutoMoqer.GetMock使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类AutoMoqer
的用法示例。
在下文中一共展示了AutoMoqer.GetMock方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestWithListener
public void TestWithListener()
{
var mocker = new AutoMoqer();
var mockConnectionFactory = mocker.GetMock<RabbitMQ.Client.ConnectionFactory>();
var mockConnection = mocker.GetMock<RabbitMQ.Client.IConnection>();
mockConnectionFactory.Setup(c => c.CreateConnection()).Returns(mockConnection.Object);
var called = new AtomicInteger(0);
var connectionFactory = new SingleConnectionFactory(mockConnectionFactory.Object);
var mockConnectionListener = new Mock<IConnectionListener>();
mockConnectionListener.Setup(m => m.OnCreate(It.IsAny<IConnection>())).Callback((IConnection conn) => called.IncrementValueAndReturn());
mockConnectionListener.Setup(m => m.OnClose(It.IsAny<IConnection>())).Callback((IConnection conn) => called.DecrementValueAndReturn());
connectionFactory.ConnectionListeners = new List<IConnectionListener>() { mockConnectionListener.Object };
var con = connectionFactory.CreateConnection();
Assert.AreEqual(1, called.Value);
con.Close();
Assert.AreEqual(1, called.Value);
mockConnection.Verify(c => c.Close(), Times.Never());
connectionFactory.CreateConnection();
Assert.AreEqual(1, called.Value);
connectionFactory.Dispose();
Assert.AreEqual(0, called.Value);
mockConnection.Verify(c => c.Close(), Times.AtLeastOnce());
mockConnectionFactory.Verify(c => c.CreateConnection(), Times.Exactly(1));
}
示例2: RequiresClientSecret
public void RequiresClientSecret()
{
var mocker = new AutoMoqer();
mocker.GetMock<IOAuthRequest>().Setup(x => x.Method).Returns(HttpMethod.Post);
mocker.GetMock<IOAuthRequest>().Setup(x => x.GrantType).Returns(GrantType.ClientCredentials);
mocker.GetMock<IOAuthRequest>().Setup(x => x.ClientId).Returns("clientid");
mocker.GetMock<IOAuthRequest>().Setup(x => x.ClientSecret).Returns<string>(null);
mocker.GetMock<IOAuthRequest>().Setup(x => x.ContentType).Returns(ContentType.FormEncoded);
var validator = mocker.Resolve<ClientCredentialsRequestValidator>();
var result = validator.ValidateRequest(mocker.GetMock<IOAuthRequest>().Object);
Assert.IsNotNull(result);
Assert.AreEqual(ErrorCode.InvalidRequest, result.ErrorCode);
Assert.IsFalse(string.IsNullOrWhiteSpace(result.ErrorDescription));
mocker.GetMock<IOAuthRequest>().Setup(x => x.ClientSecret).Returns(" ");
result = validator.ValidateRequest(mocker.GetMock<IOAuthRequest>().Object);
Assert.IsNotNull(result);
Assert.AreEqual(ErrorCode.InvalidRequest, result.ErrorCode);
Assert.IsFalse(string.IsNullOrWhiteSpace(result.ErrorDescription));
mocker.GetMock<IOAuthRequest>().Setup(x => x.ClientSecret).Returns("asdffa");
result = validator.ValidateRequest(mocker.GetMock<IOAuthRequest>().Object);
Assert.IsTrue(result.Success);
}
示例3: Setup
public void Setup()
{
this.moqer = new AutoMoq.AutoMoqer();
mockRssChannelsRepository = moqer.GetMock<IRssChannelsRepository>();
this.mockMapper = moqer.GetMock<IMapper>();
this.sut = this.moqer.Resolve<RssChannelsService>();
}
示例4: WhenAccessTokenIsExpired_ThenReturnFalse
public void WhenAccessTokenIsExpired_ThenReturnFalse()
{
var mocker = new AutoMoqer();
mocker.MockServiceLocator();
var issuer = new OAuthIssuer();
mocker.GetMock<IOAuthServiceLocator>().Setup(x => x.Issuer).Returns(issuer);
mocker.GetMock<IConfiguration>().Setup(x => x.AccessTokenExpirationLength).Returns(3600);
var validator = mocker.Resolve<ResourceRequestAuthorizer>();
var token =
issuer.GenerateAccessToken(new TokenData
{
ConsumerId = 1,
ResourceOwnerId = 5,
Timestamp = DateTimeOffset.UtcNow.AddMinutes(-65).Ticks
});
mocker.GetMock<IOAuthRequest>().Setup(x => x.AccessToken).Returns(token);
var result = validator.Authorize(mocker.GetMock<IOAuthRequest>().Object);
Assert.IsFalse(result);
}
示例5: UsesRegistryToGetNewComics
public void UsesRegistryToGetNewComics()
{
var mocker = new AutoMoqer();
var lastExplosmComic = new Comic();
var newExplosmComic1 = new Comic();
var newExplosmComic2 = new Comic();
var explosm = new Mock<IComicDownloader>();
explosm.Setup(m => m.GetNewComicsSince(lastExplosmComic))
.Returns(new[] { newExplosmComic1, newExplosmComic2 });
mocker.GetMock<IComicsRepository>()
.Setup(m => m.GetLastImportedComic(ComicType.Explosm))
.Returns(lastExplosmComic)
.Verifiable();
var registry = new ComicConfigRegistry();
registry.Add(new ComicConfig(ComicType.Explosm, explosm.Object));
// ReSharper disable once RedundantTypeArgumentsOfMethod
mocker.SetInstance<ComicConfigRegistry>(registry);
mocker.Create<ImportProcess>()
.Run();
mocker.GetMock<IComicsRepository>().VerifyAll();
mocker.GetMock<IComicsRepository>()
.Verify(m => m.InsertComic(newExplosmComic1), Times.Once);
mocker.GetMock<IComicsRepository>()
.Verify(m => m.InsertComic(newExplosmComic2), Times.Once);
}
示例6: RequiresAuthorizationCodeGrantType
public void RequiresAuthorizationCodeGrantType()
{
var mocker = new AutoMoqer();
mocker.GetMock<IOAuthRequest>().Setup(x => x.GrantType).Returns<string>(null);
var validator = mocker.Resolve<AuthorizationCodeRequestValidator>();
mocker.GetMock<IOAuthRequest>().Setup(x => x.GrantType).Returns("");
var result = validator.ValidateRequest(mocker.GetMock<IOAuthRequest>().Object);
Assert.AreEqual(ErrorCode.InvalidRequest, result.ErrorCode);
Assert.IsTrue(result.ErrorDescription.HasValue());
mocker.GetMock<IOAuthRequest>().Setup(x => x.GrantType).Returns(" ");
result = validator.ValidateRequest(mocker.GetMock<IOAuthRequest>().Object);
Assert.AreEqual(ErrorCode.InvalidRequest, result.ErrorCode);
Assert.IsTrue(result.ErrorDescription.HasValue());
mocker.GetMock<IOAuthRequest>().Setup(x => x.GrantType).Returns("bad");
result = validator.ValidateRequest(mocker.GetMock<IOAuthRequest>().Object);
Assert.AreEqual(ErrorCode.InvalidGrant, result.ErrorCode);
Assert.IsTrue(result.ErrorDescription.HasValue());
mocker.GetMock<IOAuthRequest>().Setup(x => x.GrantType).Returns(GrantType.AuthorizationCode);
mocker.GetMock<IOAuthRequest>().Setup(x => x.AuthorizationCode).Returns("authcode");
result = validator.ValidateRequest(mocker.GetMock<IOAuthRequest>().Object);
Assert.IsTrue(result.Success);
}
示例7: Setup
public void Setup()
{
_mocker = new AutoMoqer();
var mockFileSystem = new MockFileSystem();
_mocker.SetInstance<IFileSystem>(mockFileSystem);
// GetMock of the abstract class before create to prevent automoq bugs
_mocker.GetMock<FileSystemWatcherBase>();
_instance = _mocker.Create<DirectoryWatcher>();
// Mocked files
var content = new byte[] {1, 1, 1};
_expectedFileLength = content.Length;
_expectedWriteDate = DateTime.Now.ToUniversalTime();
var nameWithPath = mockFileSystem.Path.Combine(Path, FileName);
mockFileSystem.AddFile(nameWithPath, new MockFileData(content)
{
LastWriteTime = _expectedWriteDate
});
_trackedFile = new TrackedFile();
_mocker.GetMock<ITrackedFileStore>()
.Setup(x => x.GetTrackedFileByFullPath(nameWithPath))
.Returns(_trackedFile);
}
示例8: RequiresValidRedirectUrl
public void RequiresValidRedirectUrl()
{
var mocker = new AutoMoqer();
mocker.GetMock<IOAuthRequest>().Setup(x => x.ResponseType).Returns(ResponseType.Code);
mocker.GetMock<IOAuthRequest>().Setup(x => x.ClientId).Returns("12345");
var validator = mocker.Resolve<AuthorizationRequestValidator>();
var result = validator.ValidateRequest(mocker.GetMock<IOAuthRequest>().Object);
Assert.IsTrue(result.Success);
mocker.GetMock<IOAuthRequest>().Setup(x => x.RedirectUri).Returns("");
result = validator.ValidateRequest(mocker.GetMock<IOAuthRequest>().Object);
Assert.IsFalse(result.Success);
Assert.IsNotNull(result);
Assert.AreEqual(ErrorCode.InvalidRequest, result.ErrorCode);
Assert.IsFalse(string.IsNullOrWhiteSpace(result.ErrorDescription));
mocker.GetMock<IOAuthRequest>().Setup(x => x.RedirectUri).Returns("/test/whatever");
result = validator.ValidateRequest(mocker.GetMock<IOAuthRequest>().Object);
Assert.IsFalse(result.Success);
Assert.IsNotNull(result);
Assert.AreEqual(ErrorCode.InvalidRequest, result.ErrorCode);
Assert.IsFalse(string.IsNullOrWhiteSpace(result.ErrorDescription));
mocker.GetMock<IOAuthRequest>().Setup(x => x.RedirectUri).Returns("");
result = validator.ValidateRequest(mocker.GetMock<IOAuthRequest>().Object);
Assert.IsFalse(result.Success);
Assert.IsNotNull(result);
Assert.AreEqual(ErrorCode.InvalidRequest, result.ErrorCode);
Assert.IsFalse(string.IsNullOrWhiteSpace(result.ErrorDescription));
mocker.GetMock<IOAuthRequest>().Setup(x => x.RedirectUri).Returns("tcp://whatnow.com");
result = validator.ValidateRequest(mocker.GetMock<IOAuthRequest>().Object);
Assert.IsFalse(result.Success);
Assert.IsNotNull(result);
Assert.AreEqual(ErrorCode.InvalidRequest, result.ErrorCode);
Assert.IsFalse(string.IsNullOrWhiteSpace(result.ErrorDescription));
mocker.GetMock<IOAuthRequest>().Setup(x => x.RedirectUri).Returns("http://something.com");
result = validator.ValidateRequest(mocker.GetMock<IOAuthRequest>().Object);
Assert.IsTrue(result.Success);
mocker.GetMock<IOAuthRequest>().Setup(x => x.RedirectUri).Returns("https://something.com");
result = validator.ValidateRequest(mocker.GetMock<IOAuthRequest>().Object);
Assert.IsTrue(result.Success);
}
示例9: Setup
public void Setup()
{
mocker = new AutoMoqer();
mocker.GetMock<IMongoCollectionBuilder>()
.Setup(x => x.GetCollection<Sample>())
.Returns(mocker.GetMock<IMongoCollection<Sample>>().Object);
}
示例10: Init
public void Init()
{
mocker = new AutoMoqer();
guid = Guid.NewGuid();
mocker.GetMock<IGuidGetter>().Setup(a => a.GetGuid()).Returns(guid);
mocker.GetMock<IGetWorkingFolderPath>().Setup(a => a.GetPathToWorkingFolder())
.Returns("pathToWorkingFolder");
}
示例11: Setup
public void Setup()
{
mocker = new AutoMoqer();
mocker.GetMock<IMongoFactory>()
.Setup(x => x.CreateMongo())
.Returns(mocker.GetMock<IMongo>().Object);
}
示例12: TestCacheSizeExceeded
public void TestCacheSizeExceeded()
{
var mocker = new AutoMoqer();
var mockConnectionFactory = mocker.GetMock<RabbitMQ.Client.ConnectionFactory>();
var mockConnection = mocker.GetMock<RabbitMQ.Client.IConnection>();
var mockChannel1 = new Mock<IModel>();
var mockChannel2 = new Mock<IModel>();
var mockChannel3 = new Mock<IModel>();
mockConnectionFactory.Setup(c => c.CreateConnection()).Returns(mockConnection.Object);
mockConnection.Setup(c => c.CreateModel()).ReturnsInOrder(mockChannel1.Object, mockChannel2.Object, mockChannel3.Object);
mockConnection.Setup(c => c.IsOpen).Returns(true);
// Called during physical close
mockChannel1.Setup(c => c.IsOpen).Returns(true);
mockChannel2.Setup(c => c.IsOpen).Returns(true);
mockChannel3.Setup(c => c.IsOpen).Returns(true);
var ccf = new CachingConnectionFactory(mockConnectionFactory.Object);
ccf.ChannelCacheSize = 1;
var con = ccf.CreateConnection();
var channel1 = con.CreateChannel(false);
// cache size is 1, but the other connection is not released yet so this creates a new one
var channel2 = con.CreateChannel(false);
Assert.AreNotSame(channel1, channel2);
// should be ignored, and added last into channel cache.
channel1.Close();
// should be physically closed
channel2.Close();
// remove first entry in cache (channel1)
var ch1 = con.CreateChannel(false);
// create a new channel
var ch2 = con.CreateChannel(false);
Assert.AreNotSame(ch1, ch2);
Assert.AreSame(ch1, channel1);
Assert.AreNotSame(ch2, channel2);
ch1.Close();
ch2.Close();
mockConnection.Verify(c => c.CreateModel(), Times.Exactly(3));
con.Close(); // should be ignored
mockConnection.Verify(c => c.Close(), Times.Never());
mockChannel1.Verify(c => c.Close(), Times.Never());
mockChannel2.Verify(c => c.Close(), Times.AtLeastOnce());
mockChannel3.Verify(c => c.Close(), Times.AtLeastOnce());
}
示例13: GetRedirectUrl
public void GetRedirectUrl()
{
var mocker = new AutoMoqer();
var properties = new Dictionary<string, IList<string>>
{
{OAuthTokens.ResponseType, new[] {ResponseType.Code}},
{OAuthTokens.ClientId, new[]{"1"}},
{OAuthTokens.RedirectUri, new[]{"http://mydomain.com"}}
};
mocker.GetMock<IRequest>().Setup(x => x.Values).Returns(properties);
var request = new AuthorizationRequest(mocker.GetMock<IRequest>().Object, mocker.GetMock<IOAuthServiceLocator>().Object);
try
{
properties[OAuthTokens.RedirectUri] = new[] { "http://wrong.com" };
request.GetRedirectUri(new ConsumerImpl {Domain = "test.com"});
Assert.Fail("Exception not thrown");
}
catch (OAuthException ex)
{
Assert.AreEqual(ErrorCode.InvalidRequest, ex.ErrorCode);
Assert.IsTrue(ex.ErrorDescription.HasValue());
}
try
{
properties[OAuthTokens.RedirectUri] = new[] { "wrong.com" };
request.GetRedirectUri(new ConsumerImpl { Domain = "test.com" });
Assert.Fail("Exception not thrown");
}
catch (OAuthException ex)
{
Assert.AreEqual(ErrorCode.InvalidRequest, ex.ErrorCode);
Assert.IsTrue(ex.ErrorDescription.HasValue());
}
try
{
properties[OAuthTokens.RedirectUri] = new[]{"/test.com/test"};
request.GetRedirectUri(new ConsumerImpl { Domain = "test.com" });
Assert.Fail("Exception not thrown");
}
catch (OAuthException ex)
{
Assert.AreEqual(ErrorCode.InvalidRequest, ex.ErrorCode);
Assert.IsTrue(ex.ErrorDescription.HasValue());
}
properties[OAuthTokens.RedirectUri] = new[] { "http://test.com/response" };
var result = request.GetRedirectUri(new ConsumerImpl { Domain = "test.com" });
Assert.AreEqual("http://test.com/response", result);
result = request.GetRedirectUri(new ConsumerImpl { Domain = "test.com", RedirectUrl = "http://test.com/response" });
Assert.AreEqual("http://test.com/response", result);
}
示例14: RequiresAuthorizationCodeGrantType
public void RequiresAuthorizationCodeGrantType()
{
var mocker = new AutoMoqer();
mocker.GetMock<IOAuthRequest>().Setup(x => x.GrantType).Returns<string>(null);
var authorizer = mocker.Resolve<AuthorizationCodeAuthorizer>();
try
{
authorizer.Authorize(mocker.GetMock<IOAuthRequest>().Object);
Assert.Fail("Exception not thrown.");
}
catch (OAuthException ex)
{
Assert.AreEqual(ErrorCode.InvalidRequest, ex.ErrorCode);
Assert.IsTrue(ex.ErrorDescription.HasValue());
}
mocker.GetMock<IOAuthRequest>().Setup(x => x.GrantType).Returns("");
try
{
authorizer.Authorize(mocker.GetMock<IOAuthRequest>().Object);
Assert.Fail("Exception not thrown.");
}
catch (OAuthException ex)
{
Assert.AreEqual(ErrorCode.InvalidRequest, ex.ErrorCode);
Assert.IsTrue(ex.ErrorDescription.HasValue());
}
mocker.GetMock<IOAuthRequest>().Setup(x => x.GrantType).Returns(" ");
try
{
authorizer.Authorize(mocker.GetMock<IOAuthRequest>().Object);
Assert.Fail("Exception not thrown.");
}
catch (OAuthException ex)
{
Assert.AreEqual(ErrorCode.InvalidRequest, ex.ErrorCode);
Assert.IsTrue(ex.ErrorDescription.HasValue());
}
mocker.GetMock<IOAuthRequest>().Setup(x => x.GrantType).Returns("asdf");
try
{
authorizer.Authorize(mocker.GetMock<IOAuthRequest>().Object);
Assert.Fail("Exception not thrown.");
}
catch (OAuthException ex)
{
Assert.AreEqual(ErrorCode.InvalidGrant, ex.ErrorCode);
Assert.IsTrue(ex.ErrorDescription.HasValue());
}
}
示例15: WhenClientIdIsInvalid_ThenThrowsException
public void WhenClientIdIsInvalid_ThenThrowsException()
{
var mocker = new AutoMoqer();
mocker.GetMock<IOAuthRequest>().Setup(x => x.ContentType).Returns(ContentType.FormEncoded);
mocker.GetMock<IOAuthRequest>().Setup(x => x.ClientId).Returns("");
mocker.GetMock<IOAuthRequest>().Setup(x => x.ClientSecret).Returns("clientsecret");
mocker.GetMock<IOAuthRequest>().Setup(x => x.Username).Returns("username");
mocker.GetMock<IOAuthRequest>().Setup(x => x.Password).Returns("password");
mocker.GetMock<IOAuthRequest>().Setup(x => x.GrantType).Returns(GrantType.Password);
mocker.GetMock<IOAuthRequest>().Setup(x => x.ClientId).Returns("clientid");
mocker.GetMock<IConsumerRepository>().Setup(x => x.GetByClientId("clientid")).Returns<ConsumerImpl>(null);
var authorizer = mocker.Resolve<PasswordTokenRequestAuthorizer>();
try
{
authorizer.Authorize(mocker.GetMock<IOAuthRequest>().Object);
Assert.Fail("Exception not thrown");
}
catch (OAuthException ex)
{
Assert.AreEqual(ErrorCode.InvalidClient, ex.ErrorCode);
Assert.IsTrue(!string.IsNullOrWhiteSpace(ex.ErrorDescription));
}
}