0

I'm trying to test a post/create controller action in my rails 4 app, but it's failing.

Here is the scaffold generated test:

it "assigns a newly created project as @project" do
  post :create, {:project => valid_attributes}, valid_session
  assigns(:project).should be_a(Project)
  assigns(:project).should 
end

Here is the code after I refactored to use it with FactoryGirl

  it "assigns a newly created project as @project" do
    project = FactoryGirl.create(:post)
    assigns(project).should be_a(Project)
    assigns(project).should be_persisted
  end

So it's failing:

 Failure/Error: assigns(project).should be_a(Project)
   expected nil to be a kind of Project(id: integer, title: string, description: text, created_at: datetime, updated_at: datetime)

I don't know why projectis returning nil in assigns method. I already inspected it to make sure it's returning a proper Project.

Btw, here is my project factory:

factory :project do
  title "MyString"
  description "MyText"
  users {[FactoryGirl.create(:user)]}
end 

Thanks in advance!

1 Answer 1

1

The assigns(project) method call returns the value of the @project instance variable in Rails. Invoking the FactoryGirl method has no effect on this variable, so it evaluates to nil.

Sign up to request clarification or add additional context in comments.

2 Comments

So I'm supposed to extract my factory's attributes to use in valid_attributes?
Sort of. You don't need to create an instance and then "extract" them, you can just call FactoryGirl.attributes_for(..). I just noticed, though, that in your attempt to use FactoryGirl you passed in :post. Did you mean to do that rather than passing in :project? I would think you'd want post :create, {:project => FactoryGirl.attributes_for(:project)}, valid_session

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.