English 中文(简体)
Rspec Test Won t Pass usingsigns()
原标题:Rspec Test Won t Pass using assigns()

这里是我的现任用户——控制者——光谱。

require  spec_helper 

describe UsersController do
  render_views
  .
  .
  .
  describe "success" do

   before(:each) do
    @attr = { :name => "New User", :email => "[email protected]",
              :password => "foobar", :password_confirmation => "foobar" }
  end

  it "should create a user" do
    lambda do
      post :create, :user => @attr
    end.should change(User, :count).by(1)
  end

  it "should redirect to the user show page" do
    post :create, :user => @attr
    response.should redirect_to(user_path(assigns(:user)))
   end
  end
 end
end

当我执政时,我得到以下信息:

 Failures:

1) UsersController POST  create  success should redirect to the user show page
 Failure/Error: response.should redirect_to(user_path(user))
 ActionController::RoutingError:
   No route matches {:action=>"show", :controller=>"users"}
 # ./spec/controllers/users_controller_spec.rb:95:in `block (4 levels) in <top (required)> 

这使我相信:用户是实际物体。 我如何测试这一点,以及我如何改变:用户变成用户能理解的物体。

事先得到任何帮助。

<>执行>:

def create
  @title = "Sign up"
  @user = User.new(params[:user])
  if @user.save
    redirect_to @user, :notice => "Signed Up!"
  else
    @title = "Sign up"
    render "new"
  end
end

在我执政时:

it "should redirect to the user show page" do
  post :create, :user => @attr
  user = assigns(:user)
  user.should_not be_blank
  puts "user errors are: #{user.errors.full_messages.inspect}" unless user.is_valid?
  user.should be_valid
  response.should redirect_to(user_path(user))
end

我收到了:

1) UsersController POST  create  success should redirect to the user show page
     Failure/Error: user.should_not be_blank
       expected blank? to return false, got true
     # ./spec/controllers/users_controller_spec.rb:94:in `block (4 levels) in <top (required)> 
最佳回答

页: 1

response.should redirect_to(user_path(:user))

应当

response.should redirect_to(user_path(user))

Edit: Try checking that the user is valid with:

it "should redirect to the user show page" do
  post :create, :user => @attr
  user = assigns(:user)
  user.should_not be_blank
  puts "user errors are: #{user.errors.full_messages.inspect}" unless user.is_valid?
  user.应当_valid
  response.should redirect_to(user_path(user))
end

我知道,它就先前的测试案例开展工作......但在此至少需要一次广泛检查。 如果你重新确定,你就可以消除所有这种弊端。

问题回答

使用<条码>表示的方括号而不是括号。

<代码>签名[:用户]

(RSpec docs<


http://www.ohchr.org。

在测试控制器行动时,我总是使用的一种模式是,确保分配相关观点所要求的变数。 例如,

it "should assign an @user variable" do
  post :create, :user => @attr
  assigns[:user].should_not be_nil
  assigns[:user].should be_kind_of(User)
end

In your test:

 it "should redirect to the user show page" do
    @user = Factory(:user)
    post :create, :user => @attr
    user = assigns(:user)
    response.should redirect_to(user_path(@user))
  end

页: 1 你们正在创建具有“@attr”特征的用户,并声称应当改用“@user show”(不同于创建者)。 改为:

 it "should redirect to the user show page" do
    post :create, :user => @attr
    user = assigns(:user)
    response.should redirect_to(user_path(user))
  end




相关问题
Selenium not working with Firefox 3.x on linux

I am using selenium-server , selenium rc for UI testing in my application . My dev box is Windows with FireFox 3.5 and every thing is running fine and cool. But when i try to run selenium tests on my ...

Best browser for testing under Safari Mobile on Linux?

I have an iPhone web app I m producing on a Linux machine. What s the best browser I can use to most closely mimic the feature-limited version of Safari present on the iPhone? (It s a "slimmed down" ...

Code Coverage Tools & Visual Studio 2008 Pro

Just wondering what people are using for code coverage tools when using MS Visual Studio 2008 Pro. We are using the built-in MS test project and unit testing tool (the one that come pre-installed ...

Is there any error checking web app cralwers out there?

Wondering if there was some sort of crawler we could use to test and re-test everything when changes are made to the web app so we know some new change didn t error out any existing pages. Or maybe a ...

热门标签