更新:
我的测试没有提交用户信息。相关代码在上一章的练习中:
describe "after saving user" do
before { click_button submit }
let(:user) { User.find_by_email('user@example.com') }
it { should have_selector('title', text: user.name) }
it { should have_selector('div.alert.alert-success', text: 'Welcome') }
it { should have_link('Profile') }
end
/更新
我已经完成了第 8.2.5 节(注册时登录)并且应用程序的行为与描述的完全一样:
- 用户在注册时登录
- 然后重定向到他们的个人资料页面
- 其中标题已更改为包含“退出”链接。
但是我对“退出”链接的测试失败了。这是我的代码,全部从教程中复制:
相关控制器代码(users_controller.rb):
def create
@user = User.new(params[:user])
if @user.save
sign_in @user
flash[:success] = "Welcome to the Sample App!"
redirect_to @user
else
render 'new'
end
end
相关视图代码(_header.html.erb):
<ul class="dropdown-menu">
<li><%= link_to "Profile", current_user %></li>
<li><%= link_to "Settings", '#' %></li>
<li class="divider"></li>
<li>
<%= link_to "Sign out", signout_path, method: "delete" %>
</li>
</ul>
相关测试代码(user_pages_spec.rb):
describe "signup" do
before { visit signup_path }
let(:submit) { "Create my account" }
describe "with invalid information" do
it "should not create a user" do
expect { click_button submit }.not_to change(User, :count)
end
end
describe "with valid information" do
before do
fill_in "Name", with: "Example User"
fill_in "Email", with: "user@example.com"
fill_in "Password", with: "foobar"
fill_in "Confirmation", with: "foobar"
end
it "should create a user" do
expect { click_button submit }.to change(User, :count).by(1)
end
describe "after saving user" do
it { should have_link('Profile') }
end
end
end
错误是rspec ./spec/requests/user_pages_spec.rb:47 # User pages signup with valid information after saving user
谢谢!