gpt4 book ai didi

ruby-on-rails - Rspec 不改变创建计数

转载 作者:行者123 更新时间:2023-12-05 00:28:02 24 4
gpt4 key购买 nike

我正在尝试解决我的 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 的 Controller 方法定义
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

在这方面的任何帮助将不胜感激?

最佳答案

每当我们的测试给我们带来意想不到的麻烦时,退后一步并重新评估我们的方法很重要。通常,这表明存在某种设计问题,无论是我们正在测试的代码还是测试本身。

虽然听起来使用截断策略已经解决了这个特定问题(请参阅下面的更多内容),但我建议从这种情况中可以学到更多。

考虑上面规范中的两个示例。它们之间的唯一区别归结为是否code参数是否有效。我认为这些示例确实是在测试 Group 型号 ,而不是 Controller .

现在,如果我们对我们的模型测试覆盖率有信心,那么我们可以对 Controller 规范采取不同的方法。从 Controller 的角度来看,模型是一个协作者,一般来说,我们总是希望 避免 间接测试合作者。在这种情况下,我们可以使用模拟来模拟 Group 的行为。模型并仅单独测试 Controller 行为。

像这样(请注意下面的代码不完整且未经测试):

# spec/controllers/groups_controller_spec.rb
describe "#create" do

before do
# use a Test Double instead of a real model
@new_group = double(Group)
@params = { :cdb_group => 'stub_cdb_group_param', :service_id => service }
# using should_receive ensures the controller calls new correctly
Group.should_receive(:new).with(@params[:cdb_group]).and_return(@new_group)
end

context "when cancelled responding to js" do
it "renders hide_new" do
post :create, @params.merge({:button => "cancel", :format => "js"})
expect(response).to render_template('hide_new')
end
end

context "with valid params" do
before do
@new_group.should_receive(:save).and_return(true)
end

context "responding to json" # ...

context "responding to html" # ...

context "responding to xml" #...
end

context "with invalid params" do
before do
@new_group.should_receive(:save).and_return(false)
end

# ...
end

end

虽然上述内容并没有专门解决您遇到的记录计数问题,但我怀疑一旦您正确隔离了测试目标,问题可能会消失。

如果您选择坚持使用数据库截断,请考虑按照 here 所述有选择地使用它.

我希望至少有一些帮助:)。

关于ruby-on-rails - Rspec 不改变创建计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19571794/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com