2

specflow と nunit を使用して WCF サービス メソッドをテストしています。私のシナリオは次のようになります。

Feature: GetAccount
    Testing API method 'get account'

Background: 
    Given Server is running

Scenario: Succesful Get
    Given An Existing Account
    When I call the GetAccount API method With password = "123"
    Then the result should be Success

バックグラウンド ステップを実装する方法がわかりません。
サーバーは、Topshelf を使用してコンソール/Windows サービスとして実行できます。

private static void Main()
    {

      Host host = HostFactory.New(config =>
                                    {
                                      config.Service<ServiceInitializer>(service =>
                                                                      {
                                                                        service.ConstructUsing(s => new ServiceInitializer());
                                                                        service.WhenStarted((s, control) => s.Start(control));
                                                                        service.WhenStopped((s, control) => s.Stop(control));
                                                                      });
                                      config.RunAsPrompt();

                                    });
      host.Run();
    }


public class ServiceInitializer : ServiceControl

  {
    private readonly ILog m_log;

    public ServiceInitializer()
    {
      log4net.Config.XmlConfigurator.Configure();
      m_log = LogManager.GetLogger("Server");
    }


    public bool Start(HostControl hostControl)
    {
      try
      {
        var host = new IoCServiceHost(typeof(MyService));

        host.Open();
        m_log.Info("Server is now open.");

        return true;
      }
      catch (Exception exception)
      {
        m_log.Fatal("Initialization of service failed",exception);
        return false;
      }
    }


    public bool Stop(HostControl hostControl)
    {
      m_log.Info("Server has closed");
      return true;
    }

  }

.exe サービス ファイルを実行するだけでよいですか、それともServiceInitializer何らかの方法で使用できますか? おそらく私はnUnitのを使用できます[SetUpFixture]か?Specflow のベスト プラクティスはありますか?

4

1 に答える 1

1

何をテストしたいかを考えてみましょう。

  • Windowsがサービスを正しく実行していることをテストする必要がありますか?
  • Topshelfがサービスを正しく開始することをテストする必要がありますか?
  • それとも、GetAccountが機能することをテストしたいだけですか?

あなたはTopshelfを使ってあなたの生活を楽にしているに違いないので、そうして、彼らのコードがWindows内で機能することを信頼してください。コードは多くの場所で使用され、おそらく独自のテストスイートがあるため、これは有効な仮定です。仮定が間違っている場合は、後で問題が見つかったときにテストしてください。

だからあなたが本当に必要なのは

[BeforeFeature]
public void Background()
{
  FeatureContext.Current["Host"] =new MyHostObject();
}

[When("I call GetAccount API method with password =\"(\.*)\"")]
public void WhenICallGetAccount(string password)
{
  var host = (MyHostObject)FeatureContext.Current["Host"];
  ScenarioContext.Current["Account"] = host.GetAccount(password);
}

[Then("the result should be success")]
public void ThenTheResultShouldBeSuccessful()
{
  var account = (MyAccount)ScenarioContext.Current["Account"];
  //assuming using Should;
  account.ShouldNotBeNull();
}
于 2012-12-03T14:04:55.203 に答える