开发者

How to do mocks for Web tests?

I want to write a few web tests (over WatiN/Selenium + CassiniDev web server) for my asp.net web application.

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

How can i do that?

Currently i use IoC/DI container Microsoft Unity. And my pages gets his dependencies in a manner described in http://msdn.microsoft.com/en-us/library/ff664622%28v=pandp.50%29.aspx.

The only solution that comes to my head is: place all dependencies in web.config for each test case and copy necessary web.config on SetUp of test. This solution completl开发者_如何学编程y painful!

Any ideas?


I use WatiN and Cassini-dev in my integration tests as well and have had to deal with similar issues. In my setup fixture I deploy my Asp.Net web application to a temporary folder in my test folder which allows me to play around with the configuration before starting up cassini-dev. I use Windsor for my CI which allows me to change injected components at the configuration level. You may also be able to acheive this with Unity.

If the service you are referring to is a web service you just mock out a web service using the interface you have been coding to.

Here are the steps that I take when running my integration tests:

  1. Create a temp web directory
  2. Publish the Asp.Net web application to the temp directory (I use MSBuild to do this)
  3. Deploy temp database (Using MSbuild and database projects but could be done a number of ways)
  4. Deploy temp membership database (see my blog post on how to do this in code)
  5. Update the web.config of the deployed Asp.Net web application to point to the temp databases and change any other settings relevant for testing.
  6. Start up the website using Cassini-Dev. I also hit the site with a http request so that I can verify the site is up before running any tests.

Run the tests.

After running the tests you should clean up.

  1. Stop cassini-dev
  2. Delete the temp hosting folder
  3. Delete the temp databases. I use Sql server SMO objects that allow me to query the Sql Server which I use to delete up any old databases that have been left lying around after any previously failed test runs.

How to deploy a website using MSbuild in code

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 configuration section usage: http://www.pnpguidance.net/Post/UnityContainerUnityConfigurationSectionAppConfigWebConfig.aspx

Generating asp.net membership database in code: http://bronumski.blogspot.com/2011/06/generating-creating-aspnet-application.html

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


It sounds like you are trying to mock a web service. Web services usually inherit from MarshalByRefObject, this means you can create a mock by inheriting from RealProxy to create a transparent proxy that pretends to be the webservice:

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 uses RealProxy for it's mocks, so you should be able to use it to mock the web service instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜