0

My personal website is being built on rails but I'm stuck with the User.count for the login area not updating. Been sitting with this for a day trying to get it to work but no luck so far. I've included all the code if someone could spot my error as I can't see it.

 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

Then there is my User Controller

 # 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

end

And my Terminal error:

$ 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

Would appreciate anyone's help.

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

Mmkay, this is what I ended up coming up with but it's not very concise. Rake test is completing with this so I guess it will have to do.

  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

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

Edit again, the above bit of code fails on the second rake test.

3 Answers 3

0

Example with capybara and cucumber

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 => "[email protected]"
                            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
6
  • Just tried but no luck, same error when running the rake test. Dec 18, 2013 at 6:33
  • @user = users(:one) in that line, the user variable ends up beign a valid user?
    – joseramonc
    Dec 18, 2013 at 6:48
  • a little offtopic, but You can do those kinds of tests with capybara an factoryGirl, I have updated my answer and let you my user tests so you get an example of how would look like with FactoryGirl and Capybara, Have a nice one!
    – joseramonc
    Dec 18, 2013 at 7:10
  • Argh, would you believe it's not working again. It works on the first rake test but not the second. Dec 18, 2013 at 7:14
  • I hadn't, I'm pretty new to Rails. Working through the Agile Web development with Rails 4 and applying what I learn. What are the benefits? Dec 18, 2013 at 7:21
0

You have to do a

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

in your method create of UsersControlller

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

To see why it doesn't save.
Then check your user_params permit if it necessary

0

set

I18n.enforce_available_locales = false

to your config/application.rb

inside as exampled:

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

and restart server

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.