rspec テストの問題を解決してオブジェクトを作成しようとしていますが、何を試してもカウントが変わらないようです。ここで非常に基本的なものが欠けていると確信しています。
これが私のrspecです:
before do
login_account_admin(user)
@group = Factory(:group, :code => "GR_111", :description => "description for GR_111")
Group.stub!(:find).and_return(@group)
end
describe "#create" do
it "should create a new group object" do
group_params = {:code => "NEW_GROUP", :description => "description for NEW_GROUP"}
expect {
post :create, :service_id => service, :cdb_group => group_params, :button => "save", :format => "js"
}.to change(Group, :count).by(1)
end
it "should not create a new group object with invalid code format" do
group_params = {:code => "invalid", :description => "description for invalid code name group"}
expect {
post :create, :service_id => service, :cdb_group => group_params, :button => "save", :format => "js"
}.to_not change(Group, :count)
end
end
「code」パラメーターには、大文字の A から Z、0 から 9、および _ のみを含めることができます
#create のコントローラーメソッド定義は次のとおりです。
def create
@group = Group.new(params[:cdb_group])
respond_to do |format|
if params[:button] == "cancel"
format.js { render "hide_new"}
elsif @group.save
format.js {
render 'show_new_group'
}
format.html { redirect_to(some_path(@service), :notice => 'Group was successfully created.') }
format.xml { head :ok }
end
end
end
グループモデルは次のとおりです。
class Group < ActiveRecord::Base
validates_uniqueness_of :code
validates_presence_of :code, :description
validates_format_of :code, :without => /[^A-Z0-9_]/ , :message => 'can only contain uppercase letters A to Z, 0-9 and _'
end
rspec テストを実行しようとすると、次のエラーが表示されます:-
1) GroupsController User As Account Admin goes to #create should create a new group object
Failure/Error: expect {
count should have been changed by 1, but was changed by 0
# ./spec/controllers/groups_controller_spec.rb:51
2) GroupsController User As Account Admin goes to #create should not create a new group object with invalid code format
Failure/Error: expect {
count should not have changed, but did change from 2 to 1
# ./spec/controllers/groups_controller_spec.rb:58
この点で何か助けていただければ幸いです。