我正试图为我的ember-cli rails应用设置集成测试。在使用Ember内置的测试库大惊小怪之后,我改用RSpec(反正我是用它来做后端)和Capybara。虽然我终于可以正确填写表格了,但是我的post请求sign_in总是失败。我想问题是Capybara把请求发布到了不同的数据库环境或者别的什么地方。如果我在 rails 控制台检查,用户肯定在那里,而且我创建了一个用户作为 RSpec 测试的一部分。
有没有人设法设置EmberRailsCapybaraRSpec?
这是我的规范。
describe "the signin process", :type => :feature, :js => true do
it "signs me in" do
visit '/'
FactoryGirl.create :user, email: "[email protected]", password: 'password'
within("#tufts-nav") do
fill_in 'email', :with => '[email protected]'
fill_in 'password', :with => 'password'
end
click_button 'Sign In'
# here authentication fails mysteriously
expect(page).to have_content 'Jobs'
end
end
让RSpec在你的功能规格之前将ember构建到rails的public中。
# build ember, hijack rails public/ directory to host ember app
# this way no need to change settings or run any servers
# assumes rails API root path is not used (since ember now hosted from it)
RSpec.configure do |config|
public_path = Rails.root.join('public')
config.before(:context, type: :feature) do
Dir.chdir 'frontend' do
builder = spawn("ember build --environment=ci -output-path=#{public_path}")
_pid, status = Process.wait2(builder)
fail "non-zero exit status #{status}" unless status == 0
end
end
config.after(:context, type: :feature) do
`git clean -fd #{public_path}`
`git checkout #{public_path}`
end
end
我们的ember-cli应用在rails-rootfrontend中,你可能需要更改名称或路径来指向你的应用。
你可能会想尝试一下环境部分,比如使用生产环境。我这样做是因为我的生产环境是硬编码的,以我们在heroku上的API为目标,但我希望测试是自足的,即针对rails应用capybara主机运行。
需要使用Git进行清理。如果你没有Git,你可以构建到另一个路径,然后用mv把rails的public dir换掉,之后再放回去。
你可能不希望全局安装ember-cli来构建你的项目(出于版本的原因)。如果你想在本地使用项目,就把 spawn 命令指向 node_modulesember-clibinember,而不只是 ember。
如果你把ember应用当作你的rails应用的一个组件,并且想在rails级别写测试(rspec,capybara等),那么 ember-cli-rails 可能是一个不错的选择。
这个 gem 负责构建 ember 应用,并从你在 rails 路径中挂载的 urls 提供服务。
这对capybara来说是透明的:它向一个ruby webserver发送一个请求,然后返回html,调用css和js,就像rails通常做的那样。
需要注意的是,有一些 资产问题 从ember-cli得到rails服务的正确路径的那一刻,这让我换走了快速部署的东西。如果你使用rails的资产管道来处理css、图片等,那么你应该不会有问题。对我来说,它影响了ember-cli应用中的图片和webfonts。
除此之外,还需要一个api服务器和一个ember前端的服务器(代理到rails api),capybara需要被告知连接到ember前端。这个Rakefile 和 此职位 似乎是一个开始。