1

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. (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

1 Answer 1

2

@team.team_links.build is neither a persist record nor an instance variable, so the effect of this line disappeared in view context.

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.