我收到以下错误:
Capybara::ElementNotFound: Unable to find field "username"
./spec/controllers/sessions_controller_spec.rb:10:in `block (3 levels) in <top (required)>'
规格:
require 'spec_helper'
describe SessionsController do
before :each do
@user = FactoryGirl.create(:user)
end
context 'creating a new session' do
it 'can set the current_user variable to the logged user' do
visit '/login'
fill_in 'username', with: 'gabrielhilal' #I have tried `Username` as well
fill_in 'password', with: 'secret'
click_button 'Login'
current_user.should == @user
current_user.username.should == 'gabrielhilal'
end
end
context 'destroying existing session' do
xit 'can destroy the current_user' do
end
end
end
但我的格式为username
字段:
<form accept-charset="UTF-8" action="/sessions" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" / <input name="authenticity_token" type="hidden" value="x7ORLDIvq1BXr8SOkd/Zla9Pl5R5tBXAtyflCpTGCtY=" /></div>
<div class="field">
<label for="username">Username</label>
<input id="username" name="username" type="text" />
</div>
<div class="field">
<label for="password">Password</label>
<input id="password" name="password" type="password" />
</div>
<div class="actions">
<input name="commit" type="submit" value="Login" />
</div>
</form>
我使用cucumber
进行了类似的测试并且正在通过,这确认字段username
在那里:When to switch from cucumber to rspec in the BDD cycle for a login procedure
有什么想法吗?
编辑 - 我添加了save_and_open_page
,这给了我一个空白页面。 puts "#{page.html.inspect}"
也会返回empity。
""
Capybara::ElementNotFound: Unable to find field "username"
./spec/controllers/sessions_controller_spec.rb:12:in `block (3 levels) in <top (required)>'
答案 0 :(得分:6)
您可以使用以下终端进行调试:
it {puts page.body}
看看是否有“用户名”输入。
但我认为第一步visit '/login'
答案 1 :(得分:6)
我知道你已经回答了你自己的问题(有点),但我觉得有必要告诉你,你所提供的例子是控制器和请求规范的混合...
从RSpec - Controller Specs逐字引用:
Note: To encourage more isolated testing, views are not rendered by default in controller specs.
主要区别(在RSpec解释它是什么样的规范的方式)是行:describe SessionsController do
。
使其与RSpec和测试最佳实践保持一致:
spec/requests
describe "SessionsController" do
render_views
你的规格应该运行得很好......
查看RSpec - Request Specs我的意思。我还会查看betterspecs.org有关改进测试的方法。
答案 2 :(得分:0)
我意识到我必须明确告诉我的控制器渲染视图。我在rspec readme中找到了它,因此我解决了添加render_views
的问题。
describe SessionsController do
render_views #this line solved the problem
before :each do
user = FactoryGirl.create(:user)
end
...
答案 3 :(得分:0)
就我而言,只需删除form: FormGroup;
ngOnInit() {
const formControls = {
field: new FormControl('', Validators.required)
};
formControls.field.valueChanges.subscribe(() => {
const value = formControls.field.value;
// TODO: perform action based on value
});
this.form = this.formBuilder.group(formControls);
}
中的block_unknow_urls
:
features/support/env.rb