WP7 中的单元测试私有方法

发布于 2024-12-07 17:21:05 字数 2545 浏览 4 评论 0 原文

我想在 WP7 应用程序代码中为我的私有方法编写单元测试。所以我想知道如何从 Silverlight 单元测试代码中调用私有方法。下面是我想为其编写单元测试的代码片段。

    private void Next_Click(object sender, EventArgs e)
    {
        nextBtn.IsEnabled = false;
        checking = true;
        App app = Application.Current as App;
        //Microsoft.Phone.Controls.TiltEffect.SetIsTiltEnabled((App.Current as App).RootFrame, true);

        if (String.IsNullOrEmpty(AppHelper.AuthenticationToken))
        {
            // Get Authentication Token
            try
            {
                app.Flickr.AuthGetTokenAsync(frob, r =>
                {
                    Dispatcher.BeginInvoke(() =>
                    {
                        if (r.HasError)
                        {
                            MessageBox.Show("Flickr error  (" + r.ErrorMessage + ") - did you click Ok before setting flickr up in your browser?");
                        }
                        else
                        {
                            // Store the authentication token
                            AppSettings.AuthenticationToken = r.Result.Token;
                            if (AppSettings.IsLoginIconPressed == false)
                            {
                                NavigationService.Navigate(new Uri("/Upload.xaml", UriKind.Relative));
                            }
                            else
                            {
                                app.isNavigatedFromPage = true;
                                NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
                                AppSettings.IsLoginIconPressed = false;
                            }
                        }

                        checking = false;
                    });
                });
            }
            catch (Exception ex)
            {
                MessageBox.Show("Flickr error  (" + ex.Message + ") - did you click Ok before setting flickr up in your browser?");
            }

        } // End of if
        else
        {
            if (AppSettings.IsLoginIconPressed == false)
            {
                app.isNavigatedFromPage = true;
                NavigationService.Navigate(new Uri("/Upload.xaml", UriKind.Relative));
            }
            else
            {
                app.isNavigatedFromPage = true;
                NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
                AppSettings.IsLoginIconPressed = false;
            }
        } // End of else
    }

I want to write unit tests for my private methods in WP7 app code. So I would like to know how to call private methods from Silverlight Unit Test code. Below is a code snippet for which I would like to write unit test.

    private void Next_Click(object sender, EventArgs e)
    {
        nextBtn.IsEnabled = false;
        checking = true;
        App app = Application.Current as App;
        //Microsoft.Phone.Controls.TiltEffect.SetIsTiltEnabled((App.Current as App).RootFrame, true);

        if (String.IsNullOrEmpty(AppHelper.AuthenticationToken))
        {
            // Get Authentication Token
            try
            {
                app.Flickr.AuthGetTokenAsync(frob, r =>
                {
                    Dispatcher.BeginInvoke(() =>
                    {
                        if (r.HasError)
                        {
                            MessageBox.Show("Flickr error  (" + r.ErrorMessage + ") - did you click Ok before setting flickr up in your browser?");
                        }
                        else
                        {
                            // Store the authentication token
                            AppSettings.AuthenticationToken = r.Result.Token;
                            if (AppSettings.IsLoginIconPressed == false)
                            {
                                NavigationService.Navigate(new Uri("/Upload.xaml", UriKind.Relative));
                            }
                            else
                            {
                                app.isNavigatedFromPage = true;
                                NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
                                AppSettings.IsLoginIconPressed = false;
                            }
                        }

                        checking = false;
                    });
                });
            }
            catch (Exception ex)
            {
                MessageBox.Show("Flickr error  (" + ex.Message + ") - did you click Ok before setting flickr up in your browser?");
            }

        } // End of if
        else
        {
            if (AppSettings.IsLoginIconPressed == false)
            {
                app.isNavigatedFromPage = true;
                NavigationService.Navigate(new Uri("/Upload.xaml", UriKind.Relative));
            }
            else
            {
                app.isNavigatedFromPage = true;
                NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
                AppSettings.IsLoginIconPressed = false;
            }
        } // End of else
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

表情可笑 2024-12-14 17:21:05

就像其他 .Net 代码一样,您可以使用 InternalsVisibleTo< /a> 属性。

有对此的描述以及其他几个 WP7 特定内容 此处

然而,一个评论是,想要对私有方法进行单元测试通常是一种代码味道 - 精心分解的代码通常具有一个公共接口,允许彻底的单元测试,而无需专门测试私有方法。


除了对单元测试和私有方法的一般评论之外,我强烈建议您查看 MVVM 设计模式。您发现自己想要测试私有 next_click 方法的主要原因是您已将大量业务逻辑放入 UI 代码中。 MVVM 是一种将此类逻辑放入易于测试的 Model 和 ViewModel 类中的模式,Silverlight 具有多个使该模式特别易于使用的功能。

Just as with other .Net code you can use the InternalsVisibleTo attribute.

There is a description of this as well as several other WP7 specific things here.

One comment, however, is that wanting to unit test private methods is often a code smell - well factored code usually has a public interface that allows thorough unittesting without needing to specifically test the private methods.


In addition to the general comment on unit testing and private methods, I'd strongly recommend you look at the MVVM design pattern. The main reason you find yourself wanting to test the private next_click method is that you have put a whole lot of business logic within your UI code. MVVM is a pattern which places this sort of logic into easily testable Model and ViewModel classes and Silverlight has several features that make the pattern particularly easy to use.

你的他你的她 2024-12-14 17:21:05

Private 意味着您只能从当前类内部调用它,而不能从外部调用它。单元测试在外面。

如果您确实想直接测试它,请将其公开,或者使用其他方式更改其可见性。

Private means you can only call it from within the current class, not from outside. Unit-test is outside.

Make it public if you really want to test it directly, or change its visibility using other ways.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文