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.
我有一个独立的工厂女孩文件和params的职位我想做。FactoryGirl。create调用ruby创建方法,而不是控制器中的create操作。
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?
如何在RSpec中调用控制器的创建操作?我该怎么把它寄到我工厂女孩工厂里。rb文件?
posts_controller.rb
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
规范/要求/ 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
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
结束
1 个解决方案
#1
4
Request specs are integration tests, using something like Capybara to visit pages as a user might and perform actions. You wouldn't test a create
action from a request spec at all. You'd visit the new item path, fill in the form, hit the Submit button, and then confirm that an object was created. Take a look at the Railscast on request specs for a great example.
请求规范是集成测试,使用Capybara之类的方法访问页面,用户可以访问页面并执行操作。您根本不会测试来自请求规范的创建操作。您将访问新的条目路径,填写表单,单击Submit按钮,然后确认创建了一个对象。看一下Railscast的请求规范,这是一个很好的例子。
If you want to test the create action, use a controller spec. Incorporating FactoryGirl, that would look like this:
如果您想测试create操作,请使用一个控制器规范。
it "creates a post" do
post_attributes = FactoryGirl.attributes_for(:post)
post :create, post: post_attributes
response.should redirect_to(root_path)
Post.last.some_attribute.should == post_attributes[:some_attribute]
# more lines like above, or just remove `:id` from
# `Post.last.attributes` and compare the hashes.
end
it "displays new on create failure" do
post :create, post: { some_attribute: "some value that doesn't save" }
response.should redirect_to(new_post_path)
flash[:error].should include("some error message")
end
These are the only tests you really need related to creation. In your specific example, I'd add a third test (again, controller test) to ensure that the appropriate DifferentModel
record is created.
这些是您真正需要的与创建相关的测试。在您的特定示例中,我将添加第三个测试(同样是controller测试),以确保创建适当的差异模型记录。
#1
4
Request specs are integration tests, using something like Capybara to visit pages as a user might and perform actions. You wouldn't test a create
action from a request spec at all. You'd visit the new item path, fill in the form, hit the Submit button, and then confirm that an object was created. Take a look at the Railscast on request specs for a great example.
请求规范是集成测试,使用Capybara之类的方法访问页面,用户可以访问页面并执行操作。您根本不会测试来自请求规范的创建操作。您将访问新的条目路径,填写表单,单击Submit按钮,然后确认创建了一个对象。看一下Railscast的请求规范,这是一个很好的例子。
If you want to test the create action, use a controller spec. Incorporating FactoryGirl, that would look like this:
如果您想测试create操作,请使用一个控制器规范。
it "creates a post" do
post_attributes = FactoryGirl.attributes_for(:post)
post :create, post: post_attributes
response.should redirect_to(root_path)
Post.last.some_attribute.should == post_attributes[:some_attribute]
# more lines like above, or just remove `:id` from
# `Post.last.attributes` and compare the hashes.
end
it "displays new on create failure" do
post :create, post: { some_attribute: "some value that doesn't save" }
response.should redirect_to(new_post_path)
flash[:error].should include("some error message")
end
These are the only tests you really need related to creation. In your specific example, I'd add a third test (again, controller test) to ensure that the appropriate DifferentModel
record is created.
这些是您真正需要的与创建相关的测试。在您的特定示例中,我将添加第三个测试(同样是controller测试),以确保创建适当的差异模型记录。