奇怪的Shim行为C#,除非在测试方法中实例化,否则不使用Shims

时间:2022-10-16 16:11:54

this should be a simple one (i'm new to Shimming..)

这应该是一个简单的(我是Shimming的新手......)

        using(ShimsContext.Create())
        {
            ShimHttpWebRequest.Constructor = @this =>
            {
                var shim = new ShimHttpWebRequest(@this);
                shim.GetResponse = () =>
                {
                    return new ShimHttpWebResponse();
                };
            };

            ShimWebRequest.CreateString = (url) =>
            {
                return new ShimHttpWebRequest();
            };

            var http = WebRequest.Create("http://moomoo.moomoo") as HttpWebRequest;
            var r = http.GetResponse() as HttpWebResponse;
        }

So without shims, this test would fail as the there is no such url, it would fail to resolve. With the shims it works fine. Thing is, if if then create the class i want to test and invoke a method which creates an HttpWebRequest in the same way, it appears that the shim magic doesn't work, it really attempts to resolve the url. I've done a similar test with SmtpClient previously and this works, so I can't really see why my method's creation of these objects should behave any different.

所以没有垫片,这个测试会失败,因为没有这样的url,它将无法解决。使用垫片它工作正常。事情是,如果然后创建我想要测试的类并调用以相同方式创建HttpWebRequest的方法,则看起来shim magic不起作用,它确实尝试解析url。我以前用SmtpClient进行了类似的测试,这样可行,所以我无法真正理解为什么我的方法创建这些对象的行为会有所不同。

Any ideas/experience on this?

有关于此的任何想法/经验吗?

UPDATE 1

更新1

Code in my class:

我班上的代码:

    public void METHODNAME()
    {
        try
        {
            // Request the login page
            Uri url = new Uri(BaseUrl + "logon.aspx");
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.Method = "GET";
            request.AllowAutoRedirect = false;
            request.CookieContainer = Cookies;
            // Exception raised below
            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
    etc....

So it's a pretty basic method

所以这是一个非常基本的方法

UPDATE 2

更新2

Just added this into the class i am testing ON:

刚刚将这个添加到我正在测试ON的类中:

    public void test()
    {

        var http = WebRequest.Create("http://moomoo.moomoo") as HttpWebRequest;
        var r = http.GetResponse() as HttpWebResponse;
    }

It works fine.. so there must be some difference in the other method.. but i certainly can't see anything obvious. Will update when/if i find the solution

它工作得很好..所以在另一种方法中必定存在一些差异..但我当然看不到任何明显的东西。将在/如果我找到解决方案时更新

1 个解决方案

#1


3  

Your only made a shim for the string variant of from Create method. You are calling the Url variant:

你唯一的一个垫片来自Create方法的字符串变体。您正在调用Url变体:

current:

当前:

ShimWebRequest.CreateString = (url) =>
        {
            return new ShimHttpWebRequest();
        };

For Url variant of Create:

对于Create的Url变体:

ShimWebRequest.CreateUrl = (url) =>
        {
            return new ShimHttpWebRequest();
        };

#1


3  

Your only made a shim for the string variant of from Create method. You are calling the Url variant:

你唯一的一个垫片来自Create方法的字符串变体。您正在调用Url变体:

current:

当前:

ShimWebRequest.CreateString = (url) =>
        {
            return new ShimHttpWebRequest();
        };

For Url variant of Create:

对于Create的Url变体:

ShimWebRequest.CreateUrl = (url) =>
        {
            return new ShimHttpWebRequest();
        };