首页 \ 问答 \ 如何模仿猫鼬?(How to mock mongoose?)

如何模仿猫鼬?(How to mock mongoose?)

我应该在没有mongodb的node + express + mongoose + swagger app上运行测试,我需要模块来模拟mongoose(仅用于测试)。 我试过模拟mongoose和mockgoose,但我有错误:(也许我失败了..或者这个模块无法帮助我,我希望你的想法!

我创建了示例框架: https//github.com/miroslav-grabinskiy/swagger-server-mock-tested

我只需要在测试中进行模拟,而且我不需要测试mongoose,如:

Model.find()

我需要像链接一样的测试api(路由)

PS模拟猫鼬没有使用承诺:(


I should run tests on node+express+mongoose+swagger app without mongodb, I need module to mock mongoose (only for tests). I tried mock-mongoose and mockgoose, but I had errors :( Maybe I failed.. or this modules can't help me I hope for your ideas!

I created example skeleton: https://github.com/miroslav-grabinskiy/swagger-server-mock-tested

I need to mock only in tests, and I don't need to test mongoose, like a:

Model.find()

I need test api (routes) like in link

P.S. mock-mongoose not working with promises :(


原文:https://stackoverflow.com/questions/45284746
更新时间:2022-01-24 15:01

最满意答案

我创建了一个适用于我的场景的绑定转换器:换行符被替换为unicode零宽度空格字符(U + 200B):

public class IgnoreLinebreaksConverter : IValueConverter
{
    private const string Sub = " \u200B";
    private const string Lb = "\r\n";

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var s = (string)value;
        return string.IsNullOrEmpty(s) ? s : Regex.Replace(s, Lb, Sub);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var s = (string)value;
        return string.IsNullOrEmpty(s) ? s : Regex.Replace(s, Sub, Lb);
    }
}

I created a binding converter that works for my scenario: Line-breaks are replaced with the unicode zero width space character (U+200B):

public class IgnoreLinebreaksConverter : IValueConverter
{
    private const string Sub = " \u200B";
    private const string Lb = "\r\n";

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var s = (string)value;
        return string.IsNullOrEmpty(s) ? s : Regex.Replace(s, Lb, Sub);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var s = (string)value;
        return string.IsNullOrEmpty(s) ? s : Regex.Replace(s, Sub, Lb);
    }
}

相关问答

更多
  • 我不确定我理解你的问题,但我将使用“执行以下代码导致相同(性能)问题”,将i更改为300.000以向文本框添加更多文本。 改为: private void ButtonClicked(object sender, RoutedEventArgs e) { Task.Run(() => { for (int i = 0; i < 300000; i++) { tbxResult.Dispatcher.Invoke(( ...
  • 您可以在StackPanel中使用三个TextBox控件:
  • 工作解决方案:在visual studio的词典资源窗口中输入+键入似乎工作。 Working solution: shift+enter in the dictionary resource window in visual studio seems to work.
  • 在TextBox上启用TextWrapping="Wrap"和AcceptsReturn="True" 。 您也可能希望启用AcceptsTab和AcceptsTab 。 Enable TextWrapping="Wrap" and AcceptsReturn="True" on your TextBox. You might also wish to enable AcceptsTab and SpellCheck.IsEnabled too.
  • 您需要将ElementStyle添加到DataGridTextColumn,并使用它来设置TextWrapping属性: