通过与minitest的集成测试无法击中数据库

时间:2016-03-07 10:31:53

标签: ruby-on-rails testing integration-testing omniauth minitest

我一直在接受测试F

  

“User.count”未改变1。

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

测试代码是这样的:

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

  @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.product_desc,container,false);

        return view;
    }

完整输出:

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

更新说明:

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

----------------------------------------------
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 $1  [["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

1 个答案:

答案 0 :(得分:1)

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

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

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

accepts_nested_attributes_for :identities

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