I got that same error when i put the code you posted in my functions file so i wrapped it up in an if to run only once and i get the menu id using get_term_by and i assign the menu location using set_theme_mod() so here is your very own function to assign a menu to a location:
function scotts_set_nav_menu($menu_id,$location){
$locations = get_theme_mod('nav_menu_locations');
$locations[$location] = $menu_id;
set_theme_mod( 'nav_menu_locations', $locations );
}
and you can try it with this code:
$run_once = get_option('menu_check');
if (!$run_once){
$name="test";
$menu_id = wp_create_nav_menu($name);
$menu = get_term_by( 'name', $name, 'nav_menu' );
wp_update_nav_menu_item($menu->term_id, 0, array(
'menu-item-title' => 'First Menu Item',
'menu-item-url' => 'http://mysite.com',
'menu-item-status' => 'publish'));
scotts_set_nav_menu($menu->term_id,'header-menu');
update_option('menu_check', true);
}
just tried it and it works.