Factory.create 在 Put 上下文中失败,但在 Post 上下文中失败

发布于 2024-11-01 23:58:04 字数 1106 浏览 1 评论 0原文

我正在使用工厂女孩。我还是个新手,特别是对于 Rails 的测试方面。

当我描述 POST create 时,我可以创建任意数量的 Person 对象,没有任何问题。我实际上不需要在这里创建一个,因为我通常只是存根 valid?人的方法。但为了解决这个问题,我把它放在那里。无论这个 Factory.create(:person) 位于 POST 创建上下文中(或者我在那里使用它多少次),当我在 PUT 更新上下文中使用它时,它都会抛出非描述性错误消息。

此外,我已经从模型中删除了所有验证,结果没有变化。

我不知所措。

让我们开始基础工作:

describe PeopleController do
    describe "POST create" do
      describe "with valid params" do
        it "creates a person from params and renders persons/_form partial" do
            f = Factory.create(:person)
        #    f = Factory.create(:person)
        #    f = Factory.create(:person)
        end
      end
    end

    describe "PUT update" do
      describe "with valid params" do
        it "updates the requested person" do
            f = Factory.create(:person)        #error when running rake spec
        end
      end
    end
end    

错误:

 Failure/Error: f = Factory.create(:person)
 ActiveRecord::RecordInvalid:
   Validation failed: 

模型

class Person < ActiveRecord::Base
end

I'm using factorygirl. I'm still new, in particular to the testing aspect of rails.

When I descibe POST create I can create any number of Person objects without any issue. I don't actually need to create one here as I normally just stub the valid? method of Person. But for the purpose of resolving this issue I threw it in there. Regardless of this Factory.create(:person) being in the POST create context (or how many times I use it there) it throws a non-descriptive error message when I use it in the PUT update context.

Additionally I have removed all validation from the model with no change in the result.

I'm at a loss.

Lets just get the basics working:

describe PeopleController do
    describe "POST create" do
      describe "with valid params" do
        it "creates a person from params and renders persons/_form partial" do
            f = Factory.create(:person)
        #    f = Factory.create(:person)
        #    f = Factory.create(:person)
        end
      end
    end

    describe "PUT update" do
      describe "with valid params" do
        it "updates the requested person" do
            f = Factory.create(:person)        #error when running rake spec
        end
      end
    end
end    

Error:

 Failure/Error: f = Factory.create(:person)
 ActiveRecord::RecordInvalid:
   Validation failed: 

Model

class Person < ActiveRecord::Base
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最冷一天 2024-11-08 23:58:04

我很确定你们的工厂不符合验证要求。在您的工厂定义中,您的工厂应该能够通过所有验证。否则,您必须明确地满足它们,例如:

f = Factory(:person, :attribute => value_that_passes_validation, ...)

编辑(序列):

Factory.define :user do |f|
    f.sequence(:username) { |n| "test#{n}" }
    f.password "1234567890"
    f.sequence(:email) { |n| "test#{n}@test.com" }
end

I'm pretty sure that your factory does not meet the validation requirements. In your factory definition, your factory should be able to pass all the validations. Otherwise, you have to explicitly fulfil them like :

f = Factory(:person, :attribute => value_that_passes_validation, ...)

EDIT (sequences):

Factory.define :user do |f|
    f.sequence(:username) { |n| "test#{n}" }
    f.password "1234567890"
    f.sequence(:email) { |n| "test#{n}@test.com" }
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文