Trying to test a controller in Rspec. (Rails 2.3.8, Ruby 1.8.7, Rspec 1.3.1, Rspec-Rails 1.3.3)
I'm trying to post a create but I get this error message:
ActiveRecord::AssociationTypeMismatch in 'ProjectsController with appropriate parameters while logged in: should create project' User(#2171994580) expected, got TrueClass(#2148251900)
My test code is as follows:
def mock_user(stubs = {})
@user = mock_model(User, stubs)
end
def mock_project(stubs = {})
@project = mock_model(Project, stubs)
end
def mock_lifecycletype(stubs = {})
@lifecycletype = mock_model(Lifecycletype, stubs)
end
it "should create project" do
post :create, :project => { :name => "Mock Project",
:description => "Mock Description",
:owner => @user,
:lifecycletype => mock_lifecycletype({ :name => "Mock Lifecycle" }) }
assigns[:project].should == mock_project({ :name => "Mock Project",
:description => "Mock Description",
:owner => mock_user,
:lifecycletype => mock_lifecycletype({ :name => "Mock Lifecycle" })})
flash[:notice].should == "Project was successfully created."
end
The trouble comes when I try to do :owner => @user
in the code above. For some reason, it thinks that my @user is TrueClass
instead of a User
class object. Funny thing is, if I comment out the post :create
code, and I do a simple @user.class.should == User
, it works, meaning that @user is indeed a User
class object.
I've also tried
:owner => mock_user
:owner => mock_user({ :name => "User",
:email => "[email protected]",
:password => "password",
:password_confirmation => "password })
:owner => @current_user
Note @current_user is also mocked out as a user, which I tested (the same way, @current_user.class.should == User
) and also returns a TrueClass
when I try to set :owner.
Anybody have any clue why this is happening?
Thank you!