相关疑难解决方法(0)

Multiple Moq It.Is <string>()匹配参数

使用Moq,拥有多个匹配参数是否有效?

It.Is<string>() 
Run Code Online (Sandbox Code Playgroud)

在这个例子中,我希望mockMembershipService根据用户提供的内容返回不同的ProviderUserKey.

mockMembershipService.Setup(
    x => x.GetUser(
      It.Is<string>(
        s => s.Contains("Joe")))
   .ProviderUserKey)
.Returns("1234abcd");


mockMembershipService.Setup(
  x => x.GetUser(
    It.Is<string>(
      s => s.Contains("Tracy")))
  .ProviderUserKey)
.Returns("5678efgh");
Run Code Online (Sandbox Code Playgroud)

SetUp默认为第二个语句,而不是根据它自己的优点来评估每个语句.

c# unit-testing moq mocking

67
推荐指数
4
解决办法
7万
查看次数

如何在HttpSessionStateBase中正确模拟KeysCollection?

我从这里的示例设置了这个模拟会话对象:如何MOQ索引属性

/// <summary>
/// HTTP session mockup.
/// </summary>
internal sealed class HttpSessionMock : HttpSessionStateBase
{
    private readonly Dictionary<string, object> objects = new Dictionary<string, object>();

    public override object this[string name]
    {
        get { return (objects.ContainsKey(name)) ? objects[name] : null; }
        set { objects[name] = value; }
    }
}
Run Code Online (Sandbox Code Playgroud)

一些示例代码产生错误...

var mockSession = new HttpSessionMock();
var keys = mockSession.Keys;
Run Code Online (Sandbox Code Playgroud)

错误:未实现方法或操作.

我需要实现Keys属性,但不能创建KeysCollection对象.

做这个的最好方式是什么?

编辑:[解决方案]

我最终根据给出的答案更改了HttpSessionMock.这就是我最终的结果.(我还添加了对System.Linq的引用).

internal sealed class HttpSessionMock : HttpSessionStateBase
{
    private readonly NameValueCollection objects = new NameValueCollection();

    public …
Run Code Online (Sandbox Code Playgroud)

c# session unit-testing asp.net-mvc-3

8
推荐指数
1
解决办法
4167
查看次数

Moq是一个索引属性,并使用返回/回调中的索引值

我想要moq一个具有索引的属性,并且我希望能够在回调中使用索引值,就像在moq方法的回调中使用方法参数一样.可能最容易用一个例子演示:

public interface IToMoq
{
    int Add(int x, int y);
    int this[int x] { get; set; }
}

    Action<int, int> DoSet = (int x, int y) =>
    {
        Console.WriteLine("setting this[{0}] = {1}", x, y);
        throw new Exception("Do I ever get called?"); 
    };
    var mock = new Mock<IToMoq>(MockBehavior.Strict);

    //This works perfectly
    mock.Setup(m => m.Add(It.IsAny<int>(), It.IsAny<int>()))
        .Returns<int, int>((a, b) => a + b);

    //This compiles, but my callback never seems to be called
    mock.SetupSet(m => m[It.IsAny<int>()] = It.IsAny<int>())
        .Callback(DoSet);

    var obj = …
Run Code Online (Sandbox Code Playgroud)

c# moq indexed-properties

7
推荐指数
1
解决办法
1531
查看次数

Moq模拟和跟踪会话值

我在使用Moq从模拟返回Session值集时遇到问题.使用以下内容

public class TestHelpers
{
 public long sessionValue = -1;
 public HttpContextBase FakeHttpContext()
 {

  var httpContext = new Mock<HttpContextBase>();
  var session = new Mock<HttpSessionStateBase>();
  httpContext.Setup(x => x.Session).Returns(session.Object);
  httpContext.SetupGet(x => x.Session["id"]).Returns(sessionValue);
  httpContext.SetupSet(x => x.Session["id"] = It.IsAny<long>())
        .Callback((string name, object val) =>
        {
           sessionValue = (long)val;
        });
  }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试获取外部使用的值

var th = new TestHelpers();
HttpContextBase httpContext = th.FakeHttpContext();
Run Code Online (Sandbox Code Playgroud)

做一些设置Session ["id"]的东西

var sessionid = httpContext.Session["id"];
Run Code Online (Sandbox Code Playgroud)

sessionid结果为-1.但我可以使用获取设置的会话值

th.sessionValue
Run Code Online (Sandbox Code Playgroud)

怎么了?我不能简单地通过Moq返回设定值吗?

c# asp.net-mvc moq mocking

5
推荐指数
1
解决办法
7458
查看次数