I have a controller create action that creates a new blog post, and runs an additional method if the post saves successfully.
I have a separate factory girl file with the params for the post I want to make. FactoryGirl.create calls the ruby create method, not the create action in my controller.
How can I call the create action from the controller in my RSpec? And how would I send it the params in my factory girl factories.rb file?
posts_controller.rb
def create
@post = Post.new(params[:post])
if @post.save
@post.my_special_method
redirect_to root_path
else
redirect_to new_path
end
end
spec/requests/post_pages_spec.rb
it "should successfully run my special method" do
@post = FactoryGirl.create(:post)
@post.user.different_models.count.should == 1
end
post.rb
def my_special_method
user = self.user
special_post = Post.where("group_id IN (?) AND user_id IN (?)", 1, user.id)
if special_post.count == 10
DifferentModel.create(user_id: user.id, foo_id: foobar.id)
end
end
end
post_pages_spec.rblooks like a unit test on the Post model, not an actual request spec. Explicitly calling controller actions is only done in controller specs, and is done like so: relishapp.com/rspec/rspec-rails/v/2-13/docs/controller-specsmy_special_methodfor clarity. As you can see, the method counts the number ofspecial_poststhat have been saved in the database. I have a before block in my rspec that creates 9 prior posts. I want to make sure the tenth post results in the creation of a newdifferentmodelobject.