【问题标题】:Rails rspec test wheter controller action define instance variableRails rspec测试控制器动作是否定义实例变量
【发布时间】:2013-05-14 21:22:40
【问题描述】:

我有一个带动作的控制器:

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

【问题讨论】:

    标签: ruby-on-rails variables rspec controller instance


    【解决方案1】:

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

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

    # Controller
    @team_link = @team.team_link.build
    
    # Rspec
    assigns(:team_link).should_not be_empty
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多