小编Mar*_*k W的帖子

Xcode中的单元测试,是否运行应用程序?

我遇到了一个我之前没遇到的奇怪问题.

当您执行cmd + U运行单元测试(例如OCUnit)时,它是否实际调用main.m,新建appDelegate并运行应用程序,就像您按下了cmd + R一样?

我只是问,因为我在这个DataLayer后面使用CoreData.我在我的测试中成功地模拟了DataLayer,但是一旦我实现了一个实际调用CoreData的getAll方法,app/xcode抛出一个关于托管对象模型的异常不能为零.我明白这一点,但我并不是想要实际创建DataLayer类,我在mainviewcontroller loadView方法中设置了一个断点,它调用了DataLayer getAll方法.它与测试无关,因为它是一个模拟对象,但它显然是在调用真实实例.

所以回到我的问题,当按下cmd + U时,它还运行应用程序然后运行测试?

iphone xcode unit-testing ios

45
推荐指数
4
解决办法
9692
查看次数

获取字符串中字符的所有索引的更有效方法

而不是遍历每个字符以查看它是否是您想要的那个,然后将您的索引添加到列表中,如下所示:

     var foundIndexes = new List<int>();
     for (int i = 0; i < myStr.Length; i++)
     {
        if (myStr[i] == 'a')
           foundIndexes.Add(i);
     }
Run Code Online (Sandbox Code Playgroud)

c#

23
推荐指数
5
解决办法
4万
查看次数

Caliburn.Micro将它绑定到MainView中的UserControls到他们的ViewModels

我有一个MainView.xaml,绑定到MainViewModel就好了.

我想尝试的是将我在主窗体上的许多控件分成UserControls.

现在我将UserControls与MainView一起放在Views文件夹中,并命名为LeftSideControlView.xaml和RightSideControlView.xaml.相应的ViewModel位于ViewModels文件夹中,名为LeftSideControlViewModel等.

我成功将usercontrols添加到主视图:

 <Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <UserControls:LeftSideControlView cal:Bind.Model="{Binding}" />
    <UserControls:RightSideControlView cal:Bind.Model="{Binding}"
                                  Grid.Column="1" />
</Grid>
Run Code Online (Sandbox Code Playgroud)

它们在设计师中正确显示.这是xaml中的其中一个:

 <UserControl x:Class="TwitterCaliburnWPF.Library.Views.LeftSideControlView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid>
    <StackPanel>
        <Label x:Name="Text" FontSize="25" HorizontalAlignment="Center" Margin="5"/>
        <TextBox x:Name="TextBox1"
                 Width="200"
                 HorizontalAlignment="Center" FontSize="25" Margin="5" />
        <Button Width="200" Height="50" Content="Button!" Margin="20" />
    </StackPanel>
</Grid>
Run Code Online (Sandbox Code Playgroud)

我使用Castle.Windsor在AppBootstrapper for Caliburn中添加了viewmodels及其接口.

 public class ApplicationContainer : WindsorContainer
 {
  public ApplicationContainer()
  {
     // Register all dependencies here
     Register(
        Component.For<IWindowManager>().ImplementedBy<WindowManager>().LifeStyle.Is(LifestyleType.Singleton),
        Component.For<IEventAggregator>().ImplementedBy<EventAggregator>().LifeStyle.Is(LifestyleType.Singleton),
        Component.For<ILeftSideControlViewModel>().ImplementedBy<LeftSideControlViewModel>(),
        Component.For<IRightSideControlViewModel>().ImplementedBy<RightSideControlViewModel>()
        );

     RegisterViewModels();
  }

  private void RegisterViewModels()
  { …
Run Code Online (Sandbox Code Playgroud)

c# wpf caliburn.micro

13
推荐指数
1
解决办法
7002
查看次数

从UIBarButtonItem获取UIBarButtonSystemItem

尝试单元测试我在导航栏按钮上设置了正确的UIBarButtonSystemItem.

我可以恢复样式,但似乎无法找到获取UIBarButtonSystemItem的方法

为按钮设置的枚举.由于样式与枚举不同,因此失败

UIBarButtonSystemItem:

- (void)test_init_should_set_left_right_barButtonItems {

    UIBarButtonItem *left = mainVCSUT.navigationItem.leftBarButtonItem;
    UIBarButtonItem *right = mainVCSUT.navigationItem.rightBarButtonItem;
    [Assert isNotNil:left];
    [Assert isNotNil:right];

    UIBarButtonItemStyle leftStyle = left.style;
    UIBarButtonItemStyle rightStyle = right.style;

    [Assert that:[The int:leftStyle] is:[Equal to:[The int:UIBarButtonSystemItemRefresh]]];
    [Assert that:[The int:rightStyle] is:[Equal to:[The int:UIBarButtonSystemItemSearch]]];
}
Run Code Online (Sandbox Code Playgroud)

iphone cocoa-touch unit-testing ios

13
推荐指数
1
解决办法
3718
查看次数

OCMock和块测试,执行

这是测试中的方法:

- (void)loginWithUser:(NSString *)userName andPass:(NSString *)pass {

    NSDictionary *userPassD = @{@"user":userName,
                                @"pass":pass};
    [_loginCntrl loginWithUserPass:userPassD withSuccess:^(NSString *authToken){
        // save authToken to credential store
    } failure:^(NSString *errorMessage) {
        // alert user pass was wrong
    }];    
}
Run Code Online (Sandbox Code Playgroud)

我要测试的是,在该成功块中,使用适当的方法调用其他依赖项/ OCMockObject _credStore.所以目前loginCtrl和credStore依赖是OCMockObjects,我可以存根/期望这些.

在调用时,我会将loginController以某种方式执行该块吗?我已经看过一些关于使用OCMock来阻塞块的问题,我无法理解他们正在做的事情以及是否适合这种情况.

实际上我想做的就是OCMock来激活块([成功调用] ??),这样代码_credStore saveUserPass就完成了,可以在_credStore上验证.

我停在哪里:

- (void)test_loginWithuserPass_succeeds_should_call_credStore_setAuthToken {

    NSDictionary *userPassD = @{@"user":@"mark",
                                @"pass":@"test"};
    id successBlock = ^ {
        // ??? isn't this done in the SUT?
    };

    [[[_loginController stub] andDo:successBlock] loginWithUserPass:userPassD withSuccess:OCMOCK_ANY failure:OCMOCK_ANY];
    [[_credentialStore expect] setAuthToken:@"passed back value from block"];
    [_docServiceSUT loginWithUser:@"mark" andPass:@"test"]; …
Run Code Online (Sandbox Code Playgroud)

objective-c ocmock ios objective-c-blocks

11
推荐指数
2
解决办法
6335
查看次数

PLINQ查询给出溢出异常

我正在运行PLINQ查询,如下所示:

ParallelQuery<string> winningCombos = from n in nextComboMaker.GetNextCombo()
                                              .AsParallel().WithCancellation(_cancelSource.Token)
                                              where ComboWasAWinner(n) 
                                              select n;

ConcurrentBag<string> wins = new ConcurrentBag<string>();

foreach (var winningCombo in winningCombos)
{
        wins.Add(winningCombo);
        if (wins.Count == _maxWinsAllowed)
            break;
}
Run Code Online (Sandbox Code Playgroud)

GetNextCombo方法只返回下一个字母和数字组合,可能达到数十亿/万亿.

现在,当我选择一个大于Int32允许大小的组合范围时抛出异常,它总是在它运行的组合计数器为2147483584时抛出.

我已经确定GetNextCombo中没有任何东西通过创建一个假组合来每次返回(做一个收益率返回"234gf24fa23 ......"等)

LINQ抛出异常:

System.AggregateException was unhandled by user code
  Message=One or more errors occurred.
  Source=System.Core
  StackTrace:
       at System.Linq.Parallel.QueryTaskGroupState.QueryEnd(Boolean userInitiatedDispose)
       at System.Linq.Parallel.MergeExecutor`1.Execute[TKey](PartitionedStream`2 partitions, Boolean ignoreOutput, ParallelMergeOptions options, TaskScheduler taskScheduler, Boolean isOrdered, CancellationState cancellationState, Int32 queryId)
       at System.Linq.Parallel.PartitionedStreamMerger`1.Receive[TKey](PartitionedStream`2 partitionedStream)
       at System.Linq.Parallel.ForAllOperator`1.WrapPartitionedStream[TKey](PartitionedStream`2 inputStream, IPartitionedStreamRecipient`1 recipient, Boolean preferStriping, QuerySettings settings)
       at …
Run Code Online (Sandbox Code Playgroud)

c# linq plinq task-parallel-library

7
推荐指数
1
解决办法
1109
查看次数

故事板在导入xcassets后,每个图像都有问号

我的故事板中的所有图像现在都是?图片.这是在我将所有媒体资产导入新的.xcassets结构后发生的.该应用程序运行良好,图像显示应用程序运行时,但我该怎么做才能在故事板中恢复它们?

iphone ios xcode5 xcasset

7
推荐指数
1
解决办法
2036
查看次数

犀牛嘲笑告诉我AssertWasCalled内部的Arg <T>需要更多的参数吗?

这是[测试]里面的调用

_youTubeService.AssertWasCalled(d => d.GetFeedByAuthorWithRequest("Mark", Arg<YouTubeRequest>.Is.Anything));
Run Code Online (Sandbox Code Playgroud)

这是youtubeService接口的功能:

Feed<Video> GetFeedByAuthorWithRequest(string author, YouTubeRequest request)
Run Code Online (Sandbox Code Playgroud)

这是我运行测试时Rhino Mocks给出的错误:

System.InvalidOperationException:使用Arg时,必须使用Arg.Is,Arg.Text,Arg.List,Arg.Ref或Arg.Out定义所有参数.预期有2个参数,1个已被定义.

我一直使用Arg.Is.Anything与其他类型,通常是字符串,所以我不确定它还需要什么.

c# rhino-mocks mocking

6
推荐指数
1
解决办法
2889
查看次数

Rhino Mocks IgnoreArguments()并测试是否正确调用了lambda

这就是我所拥有的:

   public interface IDataCenterMsmqWriter
   {
      void UpdateData(Action<DataCenterWcfProxy> action);
   }
Run Code Online (Sandbox Code Playgroud)

被测系统:

public class WcfService : IWcfService
{
    private readonly IDataCenterMsmqWriter _writer;

    public WcfService(IDataCenterMsmqWriter writer)
    {
        _writer = writer;
    }

    #region IWcfService members

    public void SendData(SomeData data)
    {
        _writer.UpdateData(d => d.SendVarData(data));
    }

    // other members of IWcfService elided
    #endregion
}
Run Code Online (Sandbox Code Playgroud)

如何使用Rhino Mocks进行测试,将_writer设置为Mock,并希望测试在UpdateData方法中调用了正确的Action.

我试过这个:

// _writer is setup as a mock
var data = new SomeData();
_wcfServiceSUT.SendData(data);
_writer.AssertWasCalled(d => d.UpdateData(x => x.SendVarData(data));
Run Code Online (Sandbox Code Playgroud)

不起作用.

我可以添加:

,在AssertWasCalled中的UpdateData之后的p => p.I.IgnoreArguments(),但是这并没有给出我想要的东西,以确保使用数据变量调用SendVarData.

我看过这个:

如何断言一个动作被调用

但我的动作并没有像他的例子中的mockDialogService那样被嘲笑.

有没有办法测试是否使用正确的输入参数正确调用了Action或Func等?

c# unit-testing rhino-mocks

5
推荐指数
1
解决办法
1969
查看次数

UITableViewCell单元重用和UILabel自动布局动态调整大小

我正在使用子类UITableViewCell,我需要在使用自动布局时将UILabel的文本对齐到左上角.我意识到读取sizeToFit确实不应该与自动布局一起使用,我想避免它,并以某种方式使用约束.基本上,每次重复使用单元时都会重置标签的文本,因此在重用时,大小调整需要是动态的.

这是子类化单元格内的Lazy初始化程序标签:

- (UILabel *)commentsLabel {

    if (_commentsLabel == nil) {

         _commentsLabel = [[UILabel alloc] init];
         [_commentsLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
         _commentsLabel.numberOfLines = 0;
         _commentsLabel.lineBreakMode = NSLineBreakByWordWrapping;
         _commentsLabel.preferredMaxLayoutWidth = 100;
    }

    return _commentsLabel;
}
Run Code Online (Sandbox Code Playgroud)

标签上设置了自动布局约束(commentsLabel是添加到单元子类上的self.customView的子视图):

[self.customView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-locationToTopPadding-[locationLabel(locationHeight)]-locationToCommentsPadding-[commentsLabel]-commentsToBottomPadding-|"
                                                                        options:0
                                                                        metrics:@{
                                                                                  @"locationToTopPadding":@(locationToTopPadding),
                                                                                  @"locationHeight":@(locationHeight),
                                                                                  @"locationToCommentsPadding":@(locationToCommentsPadding),
                                                                                  @"commentsToBottomPadding":@(commentsToBottomPadding)
                                                                                  }
                                                                          views:viewsDictionary]];

[self.customView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[thumbnailImageView]-[commentsLabel]-|"
                                                                        options:0
                                                                        metrics:@{@"commentsWidth":@(commentsWidth)}
                                                                          views:viewsDictionary]];
Run Code Online (Sandbox Code Playgroud)

设置只是:

self.commentsLabel.preferredMaxLayoutWidth = 100;
Run Code Online (Sandbox Code Playgroud)

虽然在大多数答案中提到过,但似乎没有用.

目前有这个实施但没有看到任何结果. UILabel sizeToFit不适用于autolayout ios6

我试过的另一个答案. UILabel sizeToFit仅适用于关闭AutoLayout

我觉得我只缺少1个约束,除了上面的约束之外还可以添加,但我尝试添加的程序性约束不起作用或抛出异常.我完全在代码中工作没有xib.

ETA:尝试在现有垂直约束线内设置高度约束:

像这里建议:动态改变UILabel宽度不适用于autolayout

在上面的垂直约束线中:

-[commentsLabel(>=30@900)]-
Run Code Online (Sandbox Code Playgroud)

我已经弄乱了高度值和优先级值,没有任何改变.

ETA:取得一些进展,我认为它与标签的底部填充有关,试过这个并且一些标签正确对齐有些不是:

->=commentsToBottomPadding-
Run Code Online (Sandbox Code Playgroud)

iphone objective-c uitableview ios autolayout

5
推荐指数
1
解决办法
4146
查看次数