Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails rspec测试wheter控制器动作定义实例变量_Ruby On Rails_Variables_Rspec_Controller_Instance - Fatal编程技术网

Ruby on rails Rails rspec测试wheter控制器动作定义实例变量

Ruby on rails Rails rspec测试wheter控制器动作定义实例变量,ruby-on-rails,variables,rspec,controller,instance,Ruby On Rails,Variables,Rspec,Controller,Instance,我有一个控制器,可以执行以下操作: def new @team = Team.new @team.team_links.build end 我想用rspec测试这个动作。(我知道它可以工作,因为它在应用程序中正常工作,但我的规格不合格。)以下是我的规格: it "returns new team with built in team_link" do get 'new' team = assigns(:team) team.should be_new_record t

我有一个控制器,可以执行以下操作:

def new
  @team = Team.new
  @team.team_links.build
end
我想用rspec测试这个动作。(我知道它可以工作,因为它在应用程序中正常工作,但我的规格不合格。)以下是我的规格:

it "returns new team with built in team_link" do
  get 'new'

  team = assigns(:team)
  team.should be_new_record
  team.team_links.should_not be_empty # HERE IS WHERE IT FAILS
end
以下是错误消息:

 1) TeamsController GET new returns @team and builds one team_link
     Failure/Error: team.team_links.should_not be_empty
       expected empty? to return false, got true

@team.team\u links.build
既不是持久记录也不是实例变量,因此此行的效果在视图上下文中消失

您需要将其指定为实例变量,以便在视图中进行测试:

# Controller
@team_link = @team.team_link.build

# Rspec
assigns(:team_link).should_not be_empty