0

我正在尝试运行此测试,但由于某种原因它失败了。

describe "User Pages" do
    subject { page }
    .
    .
describe "Sign up" do
    before { visit signup_path }

    let(:submit) { "Sign Me Up" }
    .
    .
 describe "with valid information" do
  before do
    fill_in "name",             with: "newuser"
    fill_in "Username",         with: "user254"
    fill_in "Email",            with: "user@yahoo.com"
    select  "Male",             from: "gender"        
    fill_in "Password",         with: "password"
    fill_in "Confirm Password", with: "password"
  end

  it "Should create a user" do
    expect { click_button submit}.to change(User, :count).by(1)
  end   
end

但是,当我运行测试时,出现此错误

Failures:

  1) User Pages signup with valid information Should create a user
      Failure/Error: expect { click_button submit}.to change(User, :count).by(1)
       count should have been changed by 1, but was changed by 0
      # ./spec/requests/user_pages_spec.rb:35:in `block (4 levels) in <top (required)>'

      Finished in 4.61 seconds
      35 examples, 1 failure

      Failed examples:

      rspec ./spec/requests/user_pages_spec.rb:34 # User Pages signup with valid information Should create a user

这是我在控制器中的创建操作

def create
   @user = User.new(params[:user])
   if @user.save
     redirect_to @user
   else
     render 'new'
   end
end

当我尝试在浏览器中注册时,注册成功。我的测试有什么问题吗?谢谢

4

2 回答 2

1

检查用户是否被保存在控制器上:

def create
   @user = User.new(params[:user])
   puts "valid? = #{@user.valid?.inspect}"
   if @user.save
     puts 'saved!'
     redirect_to @user
   else
     puts 'not saved'
     puts @user.errors.inspect
     render 'new'
    end
end

这将打印控制器进程,你会知道发生了什么

如果什么都没有打印,那么问题是一些 before_filter 阻止了创建操作被调用

于 2013-01-21T11:54:38.770 回答
0

试试看

it "Should create a user" do
    click_button submit
    User.count.should == 1
  end  
于 2013-01-20T19:04:26.530 回答