我有一个带动作的控制器:
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
答案 0 :(得分:2)
@team.team_links.build
既不是持久记录也不是实例变量,因此该行的效果在视图上下文中消失。
您需要将其指定为可在视图中测试的实例变量:
# Controller
@team_link = @team.team_link.build
# Rspec
assigns(:team_link).should_not be_empty