How to make mocks for web tests?

I want to write some web tests (via the WatiN / Selenium + CassiniDev web server) for my asp.net web application.

The problem that I encountered is that I do not know what to do in such situations: there is a page on which the user can click a button to call a third-party service. In my web test, I want to create a mock of this service that will always return a static value (some value in this test case and another value in another test case).

How can i do this?

I am currently using the Microsoft Unity IoC / DI container. And my pages get its dependencies in the manner described in http://msdn.microsoft.com/en-us/library/ff664622%28v=pandp.50%29.aspx .

The only solution that comes to my mind: put all the dependencies in web.config for each test case and copy the required web.config to the SetUp of the test. This decision is completely painful!

Any ideas?

+3
source share
2 answers

I use WatiN and Cassini-dev in my integration tests, and I have had to deal with similar problems. In my configuration device, I deploy my Asp.Net web application to a temporary folder in the test folder, which allows me to play with the configuration before running cassini-dev. I use Windsor for my CI, which allows me to modify the input components at the configuration level. You can also achieve this with Unity .

, , -, -, , .

, :

  • - temp
  • - Asp.Net temp ( MSBuild)
  • ( MSbuild , )
  • temp (. , )
  • web.config - Asp.Net, temp , .
  • Cassini-Dev. HTTP-, , .

.

.

  • cassini-dev
  • temp. SMO Sql, Sql-, , - .

- MSbuild

var properties = new Dictionary<string, string>
{
    {"Configuration", isDebug ? "Debug" : "Release"},
    {"WebProjectOutputDir", tempHostingDirectory.FullName},
    {"DeployToDatabase", "true"},
    {"OutDir", Path.Combine(tempHostingDirectory.FullName, "bin\\")}
};

using (var engine = new ProjectCollection(properties))
{
    engine
        .LoadProject(<web project path>, "4.0")
                .Build(new[] {"Build", "ResolveReferences", "_CopyWebApplication"});
}

Unity: http://www.pnpguidance.net/Post/UnityContainerUnityConfigurationSectionAppConfigWebConfig.aspx

asp.net : http://bronumski.blogspot.com/2011/06/generating-creating-aspnet-application.html

Msbuild ProjectCollection MSDN: http://msdn.microsoft.com/en-us/library/microsoft.build.evaluation.projectcollection.aspx

+2

, -. - MarshalByRefObject, , RealProxy -, -:

class Mock : RealProxy
{
    public Mock()
        : base(typeof(IStuff)) { }

    public IStuff GetStuff()
    {
        return (IStuff)GetTransparentProxy();
    }

    public override IMessage Invoke(IMessage msg)
    {
        IMethodCallMessage message = (IMethodCallMessage)msg;

        // the message object provides the MethodInfo that was called
        // as well as the arguments.

        // <Insert logic here>

        return new ReturnMessage(new NotImplementedException("comming soon to a test near you ..."), message);
    }
}

I belieave NMock2 RealProxy , , -.

0

Source: https://habr.com/ru/post/1785618/


All Articles