在rails中测试的问题

时间:2016-02-23 05:09:44

标签: ruby-on-rails testing

我正在为我的应用程序编写测试。我写了很多测试,他们工作正常。但是,我的两个测试仍然失败,我不确定为什么会这样。第一个测试是对表单的编辑失败。 测试如下:

ScoreboardEditTest#test_unsuccessfull_editing_of_the_scoreboard [/home/ubuntu/workspace/test/integration/scoreboard_edit_test.rb:13]:
expecting <"scoreboards/edit"> but rendering with <[]>

下面给出了与此测试相关的错误。

def update
  @scoreboard = Scoreboard.find(params[:id])
  if @scoreboard.update_attributes(scoreboard_params)
    redirect_to @scoreboard
    flash[:success] = "Updated Successfully"
  else
   render 'edit'
  end
 end

下面给出了以下测试的控制器。

test "valid creation of the scoreboard with passing validations" do
    log_in_as(@user)
    get new_scoreboard_path
    assert_difference 'Scoreboard.count', 1 do
      post scoreboards_path, scoreboard: {name_of_scoreboard: "abc",
                                          name_of_organization: "def",
                                          name_of_activity: "ghi" }
      end
      assert_redirected_to scoreboard_path(@scoreboard) (doesn't work)
      assert_equal 'Scoreboard created successfully', flash[:success]
  end

获得edit_scoreboard_path后,编辑记分板模板应显示出来。我不确定为什么它会给我错误。我和用户模型完全相同,它的工作正常。我认为我在理解它的过程中遗漏了一些东西。

第二项测试是记分板的有效创建。测试如下。

Expected response to be a redirect to <http://www.example.com/scoreboards/1> but was a redirect to <http://www.example.com/scoreboards/941832920>.

重定向到错误的记分板ID。在灯具中我将id设置为1.错误信息如下所示。

- (void) getRoute
{
     polyline.map = nil;
     [googleMapView clear];
     NSString *strUrl = [NSString   stringWithFormat:@"https://maps.googleapis.com/maps/api/directions/json?&alternatives=true&origin=%@&destination=%@&key=%@&sensor=false",self.sourceText.text,self.destnText.text,googleAPI_Key];

     [self routeDataUrl1:strUrl];
     [self.view addSubview:self.getInfo];
     [self.view addSubview:customIndicatorView]; 
}

-(void) connectionDidFinishLoading:(NSURLConnection *)connection
{
     if (webData) //webData is object of NSData
     {
         [self gettingData:webData ]; //calling the method gettingData
     }

     [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
     webData=nil;
}

//after that gettingData method

- (void) gettingData:(NSData *)data
{

    NSDictionary *allDataDictionary = [NSJSONSerialization 
                                  JSONObjectWithData:data options:0 
                                  error:nil];

    if ([[NSString stringWithFormat:@"%@",[allDataDictionary objectForKey 
    : @"status"]] isEqualToString: @"OK"]) 

    {

        collectionArray =[[NSMutableArray alloc]init];
        NSMutableArray *legsArray = [[NSMutableArray alloc] initWithArray:allDataDictionary[@"routes"]];


        for (int i=0; i< [legsArray count]; i++)
        {

            [collectionArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:legsArray[i][@"legs"][0][@"distance"][@"text"],@"Distance", legsArray[i][@"legs"][0][@"duration"][@"text"],@"Duration",legsArray [i][@"summary"],@"Address",legsArray[i][@"legs"][0][@"summary"][@"html_instructions"][@"text"],@"HtmlInstructions",legsArray[i][@"overview_polyline"][@"points"],@"OverviewPolyline", nil]];
            [polyLineArray addObject:legsArray[i][@"legs"][0][@"steps"]];
            NSLog(@"11 == %@",legsArray[i][@"legs"][0][@"steps"]);         
        }    
        [self.getInfo reloadData];  //where getInfo is the TableView
    }
    else
    {
        NSString *msg=[allDataDictionary objectForKey:@"error_message"];
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Error !" message:msg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

        [alert show];
    }
}

我不确定这究竟是什么意思。正如我所提到的,我在灯具中设置了ID。我甚至手动将id设置为'941832920'。它仍然给了我一个错误。不知道为什么这样做。

1 个答案:

答案 0 :(得分:0)

对于第二次测试,我认为你不能设置一个id,它会在保存记录时由数据库分配。检查您是否被定向到正确的记分板路径的更好方法是首选格式:assert_redirected_to scoreboard_path(assigns(:scoreboard))