无法命中数据库,通过 minitest 集成测试

Can't hit the DB, by integration testing with minitest

我不断得到测试 F

"User.count" didn't change by 1.

即使我不应该这样做(因为 SessionsController#create 确实已经有效)。

测试代码是这样的:

- [REFACTORED/UPDATED] - 我还放弃了模型中的所有验证,只是为了看看......但什么都没有

class UsersSignupTest < ActionDispatch::IntegrationTest

  test "valid signup information" do   

    OmniAuth.config.test_mode = false

    get signup_path
    assert_difference 'User.count', 1 do
      get '/auth/identity/register', params: {
        user: { name: "Example User",
                email: "user@example.com",
                password_digest: "password",
                identities: { user_id: 1, 
                              provider: "identity", 
                              uid: "1"}}}
    end

    OmniAuth.config.test_mode = true
  end
end 

完整输出:

UsersSignupTest#test_valid_signup_information = 0.69 s = F


Failure:
UsersSignupTest#test_valid_signup_information [/home/lsoave/rails5/gitwatcher/test/integration/users_signup_test.rb:19]:
"User.count" didn't change by 1.
Expected: 5
  Actual: 4

tail -f log/test.log 我得到以下信息:

----------------------------------------------
UsersSignupTest: test_valid_signup_information
----------------------------------------------
Started GET "/signup" for 127.0.0.1 at 2016-03-07 15:17:16 +0100
Processing by UsersController#new as HTML
  Rendered users/new.html.erb within layouts/application (37.7ms)
  User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" IS NULL LIMIT   [["LIMIT", 1]]
  Rendered layouts/_navigation.html.erb (3.6ms)
  Rendered layouts/_signup-modal.html.erb (2.6ms)
  Rendered layouts/_messages.html.erb (0.4ms)
  Rendered layouts/_footer.html.erb (0.2ms)
Completed 200 OK in 265ms (Views: 250.3ms | ActiveRecord: 0.6ms)
   (0.4ms)  SELECT COUNT(*) FROM "users"
Started GET "/auth/identity/register?user[name]=Example+User&user[email]=user%40example.com&user[password_digest]=[FILTERED]&user[identities][user_id]=1&user[identities][provider]=identity&user[identities][uid]=1" for 127.0.0.1 at 2016-03-07 15:17:16 +0100
   (0.3ms)  SELECT COUNT(*) FROM "users"
   (0.2ms)  ROLLBACK

更新说明:

真正的模型是 User has_many identities / Identity belong_to 用户关系,我不确定它是否正确表示为 :

...
params: {
            user: { name: "Example User",
                    email: "user@example.com",
                    password_digest: "password",
                    identities: { user_id: 1, 
                                  provider: "identity", 
                                  uid: "1"}}}
...

尝试用以下内容替换身份:片段:

identities_attributes: { '0': {provider: "identity", uid: "1"}}

并检查您的用户模型包含:

accepts_nested_attributes_for :identities

无需自己处理将 user_id 分配给依赖模型,这就是 Rails 擅长的。