简体   繁体   English

Rails Rspec测试者控制器动作定义实例变量

[英]Rails rspec test wheter controller action define instance variable

I have a controller with action: 我有一个动作控制器:

def new
  @team = Team.new
  @team.team_links.build
end

And I want to test this action with rspec. 我想用rspec测试此操作。 (I know it works, because it works as it should in the application, but my specs are failing.) Here is my spec: (我知道它可以正常工作,因为它可以在应用程序中正常工作,但是我的规格失败了。)这是我的规格:

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

Here is the error message: 这是错误消息:

 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_links.build is neither a persist record nor an instance variable, so the effect of this line disappeared in view context. @team.team_links.build既不是持久记录也不是实例变量,因此该行的效果在视图上下文中消失了。

You need to assign it as an instance variable to be testable in view: 您需要将其分配为实例变量以便在视图中可测试:

# Controller
@team_link = @team.team_link.build

# Rspec
assigns(:team_link).should_not be_empty

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM