Rails - User.count没有改变1

时间:2013-12-18 01:16:44

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4

我的个人网站是在rails上构建的,但是我坚持使用User.count来登录区域不更新。一直坐在这一天试图让它工作,但到目前为止没有运气。如果有人能看到我的错误,我已经包含了所有代码,因为我看不到它。

 class UsersControllerTest < ActionController::TestCase
  setup do
    @user = users(:one)
    @input_attributes = {
      name: 'luchia',
      password: 'secret',
      password_confirmation: 'secret' 
    }
  end

  test "should create user" do
   assert_difference('User.count') do
    post :create, user: @input_attributes
  end
  assert_redirected_to users_path
  end

然后是我的用户控制器

 # GET /users/1
 # GET /users/1.json
 def show
 end

 # GET /users/new
 def new
  @user = User.new
 end

 # GET /users/1/edit
 def edit
 end

 # POST /users
 # POST /users.json
 def create
  @user = User.new(user_params)

 respond_to do |format|
  if @user.save
    format.html { redirect_to users_url, notice: "User #{@user.name} was successfully created." }
    format.json { render action: 'show', status: :created, location: @user }
  else
    format.html { render action: 'new' }
    format.json { render json: @user.errors, status: :unprocessable_entity }
  end
end

我的终端错误:

$ rake test
Run options: --seed 11633

# Running tests:

....[deprecated] I18n.enforce_available_locales will default to true in the future. If   you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
....................F......

Finished tests in 1.143021s, 27.1211 tests/s, 45.4935 assertions/s.

1) Failure:
UsersControllerTest#test_should_create_user [/Users/lucybloomfield/Documents/luchia /test/controllers/users_controller_test.rb:25]:
"User.count" didn't change by 1.
Expected: 3
Actual: 2

31 tests, 52 assertions, 1 failures, 0 errors, 0 skips

感谢任何人的帮助。

////////////////

Mmkay,这是我最终想出来的,但它不是很简洁。耙子测试正在完成,所以我想它必须这样做。

  test "should create user" do
   assert_difference('User.count') do
    post :create, {:user => {'name' => 'luchia', 'password' => 'secret', 'password_confirmation' => 'secret'}}
    .to change(User, :count).by(1)
   end
   assert_redirected_to users_path
  end

//////////////

再次编辑,上面的代码在第二次rake测试中失败。

3 个答案:

答案 0 :(得分:0)

水豚和黄瓜的例子

describe "new" do
            before do 
                    visit new_user_path
            end

            it { should have_title(I18n.t("user.new")) }

            describe "with invalid information" do
                    before { find("form#new_user").submit_form! }

                    it { should have_title(I18n.t("user.new")) }
                    it { should have_selector("div#alerts") }
            end

            describe "with valid information" do
                    before do
                            fill_in "user_name", :with => "user_name"
                            fill_in "user_email", :with => "user@example.com"
                            fill_in "user_password", :with => "123456"
                            fill_in "user_password_confirmation", :with => "123456"
                            find("form#new_user").submit_form!
                    end

                    it { should have_content(I18n.t("user.success"))}
            end
    end

答案 1 :(得分:0)

你必须做一个

puts YAML::dump(@user.save!)

在您的方法中创建UsersControlller

def create
  @user = User.new(user_params)
  puts YAML::dump(@user.save!)
...
end

了解为什么不保存。
然后检查您的user_params许可证是否必要

答案 2 :(得分:0)

I18n.enforce_available_locales = false

到你的config / application.rb

内部例如:

module MyApp
  class Application < Rails::Application
    ...
    I18n.enforce_available_locales = false
  end
end

并重启服务器