1

以下に、IIS7から設定を読み取ろうとし、残念ながらナイトリービルドマシンにIIS7がないため、単体テストできないコードがいくつかあります。私が考えることができる唯一のことは、ServerManagerをメソッドに渡すことですが、呼び出し元には、そのメソッドを単体テストできないようにするServerManagerがあります。モックライブラリにはMOQを使用しています。

        public ISection GetCurrentSettings(string location, Action<string> status)
    {
        #region Sanity Checks

        if (string.IsNullOrEmpty(location))
        {
            throw new ArgumentNullException("location");
        }
        if (status == null)
        {
            throw new ArgumentNullException("status");
        }
        #endregion

        ISection section = null;

        _logger.Debug(string.Format("Retrieving current IIS settings for app at {0}.", location));
        status("Getting current IIS settings.");
        using (ServerManager manager = new ServerManager())
        {
            var data = (from site in manager.Sites
                        from app in site.Applications
                        from vdir in app.VirtualDirectories
                        where vdir.PhysicalPath.Equals(location, StringComparison.CurrentCultureIgnoreCase)
                        select new {Website = site, App = app}).SingleOrDefault();

            if (data == null)
            {
                _logger.Debug(string.Format("Could not find an application at {0} in IIS. Going to load the defaults instead.", location));
                //ToDo possibly load defaults
            }
            else
            {
               _logger.Debug(string.Format("Application found in IIS with website: {0} and a path of {1}", data.Website.Name, data.App.Path));
                int port =
                    data.Website.Bindings.Where(b => b.EndPoint != null).Select(b => b.EndPoint.Port).Single();


                section = new IISSection
                    {
                        ApplicationPoolName = data.App.ApplicationPoolName,
                        VirtualDirectoryAlias = data.App.Path,
                        WebsiteName = data.Website.Name,
                        WebsiteRoot = data.App.VirtualDirectories[0].PhysicalPath,
                        Port = port.ToString(CultureInfo.InvariantCulture),
                        WillApply = true,
                        AnonymousUser = _userService.GetUserByType(UserType.Anonymous)
                    };
            }

            return section;

        }
4

2 に答える 2

3

コードを完全に書き直すことなく、一般的な考え方は、IISから必要なデータを取得するメソッドを公開するISettingReader *(IisSettingReaderとして実装)を渡すことです。次に、ISettingReaderをメソッド/クラスに渡すことにより、ISettingReaderをスタブして必要なものを返すことができます。

*または、IServerManagerが現在の名前のようですが、それがIIS固有であるかどうかはわかりません

アップデート

具体的には、Darin Dimitrovが詳しく説明したように、すべての依存関係をメソッドの外部にプルし、パラメーター/コンストラクター/プロパティインジェクションを介してそれらを渡す必要があります。これには、現在の状態であるため、コードを書き直す必要があります。

そうでない場合(そして私は書き直しを提案します)、TypeMockのようなものを使用できます。これはおそらくクラス内の依存関係を偽造する可能性がありますが、私はこれを自分で使用したことはなく、何を読んだかしかわかりません。

于 2012-09-24T16:28:07.257 に答える
0

Moqを使用します。

これにより、実際のバージョンを作成するのではなく、モックバージョンのISettingsを作成できます。また、独自の機能を指定できるという追加の利点もあります。

于 2012-09-24T16:27:55.820 に答える