功能测试很容易做到。在用户示例中,我会将它们放入spec/controllers/users_controller_spec.rb
Rspec 中:
require 'spec_helper'
describe UsersController do
render_views # if you have RABL views
before do
@user_attributes = { email: "test@example.com", password: "mypass" }
end
describe "POST to create" do
it "should change the number of users" do
lambda do
post :create, user: @user_attributes
end.should change(User, :count).by(1)
end
it "should be successful" do
post :create, user: @user_attributes
response.should be_success
end
it "should set @user" do
post :create, user: @user_attributes
assigns(:user).email.should == @user_attributes[:email]
end
it "should return created user in json" do # depend on what you return in action
post :create, user: @user_attributes
body = JSON.parse(response.body)
body["email"].should == @user_attributes[:email]
end
end
显然,您可以优化上述规格,但这应该可以帮助您入门。干杯。